so I was writing some matrix classes in C++. So each matrix has a pointer to an array called entries, I am not sure if i’m doing this right but I am redeclaring the array in the sub class. (I’m no expert in C++)
Does this memory need to be free’d? Am I simply overwriting the pointer with the referenced array? Any help would be greatly appreciated.
Thanks
class Matrix {
protected:
float* entries;
public:
int rows;
int cols;
Matrix() {
}
~Matrix() {
}
};
class Matrix4x4 : public Matrix {
protected:
float entry[4][4];
public:
/* This will create an empty matrix */
Matrix4x4() {
//Define the size of the arrays
rows = 4;
cols = 4;
this->empty();
}
...
};
The code isn’t leaking memory and cannot possibly do so, since you never say
newormallocanywhere in your code*, nor do you call functions which do so in an unguarded fashion (i.e. outside RAII objects).However, the code is also fairly useless and misguided (e.g.
Matrix4x4::entryis hiding the vestigialMatrix::entry, androwsandcolumnsseem to be somewhat redundant variables), and that can only be solved by sitting down with a good C++ book for a few hours.*) or, as @Nicol Bolas has helpfully pointed out,
make_sharedtogether with a lost circular reference