Memory allocation (deletion)

So I am trying to figure out how I should properly delete a matrix. I believe the code I have correctly allocates memory and sets it up correctly if I type something like Matrix a(2,2). However, I want to know how to delete Matrix a and only Matrix a if I have many matrices. Assume a proper header file.

Matrix::Matrix(uint rows, uint cols){
array = (double **)calloc(rows,sizeof(double *));
array = new double*[rows];

for(uint i = 0; i < rows; i++){
array[i] = (double *)calloc(cols,sizeof(double *));
array[i] = new double[cols];
}

for(uint i = 0; i < rows; i++){
for(uint j = 0; j < cols; j++){
array[i][j] = 0.0;
}
}

Matrix::~Matrix(){//What can I do to make this function work correctly? }
You don't actually ever call the explicit destructor, even though you write one out (I know, confusing, right?). Instead, you call delete [] array;. Since your "array" is a pointer to an array of type double, essentially, it dereferences the location in RAM in which the type double array is stored, and you're no longer able to access it.

I forget if it's the program closing or the OS that cleans it up afterwards, but that part shouldn't matter at this point.

Btw am I correct in assuming that, when you say Matrix "a" you mean your Maxtrix "array"?
So the tough part about this assignment is that were not allowed to use the array class, that's why I'm doing it as I am. And I do mean Matrix a. In my header file I set up aa constructor and in the code I gave I'm trying to construct a new array using pointers. I'm very new to pointers so I really have no idea if I did it right.
If you're allocating memory with the calloc function, use the complementary free function. I'd recommend using either new or delete and not mixing both.

I don't understand what you're asking. Are you asking how to use constructors or how to free dynamically allocated memory?
Well you'll never free the memory successfully because your constructor leaks right now. First you allocate memory with calloc(), as assign it to array, then you allocate memory with new and assign it to array. After this, nothing points to the memory that was assigned by calloc() so it can't be freed. The same thing happens when you allocate memory for each row.

To fix the leak, get rid of the calls to calloc(). You don't need them.

To free the memory, delete the memory for each row and then free the array of rows. It'sort of the reverse of the constructor. But to do that, you need to remember the number of rows in the array and I don't see your constructor doing that. You will probably need to remember the number of columns too (for math operations on the array).
I guess I'm a bit confused as far as handling memory goes. So say I have a very simple header file called "Matrix.h"

Matrix.h
class Matrix{
public:
Matrix(uint rows, uint cols);
~Matrix();

How would I write Matrix.cpp to correctly implement these two functions to create and destroy different matrices. So say I type in another program that imports Matrix.cpp:

//assume proper imports and main setup
Matrix a(2,2);
Matrix b(2,1);
~Matrix a;
~Matrix b;

Is this something I can do, how can I do it without leaking memory? Also, I cannot use the array class

1
2
3
4
Matrix a(2,2);
Matrix b(2,1);
~Matrix a;
~Matrix b;

You normally (like almost never) call the destructor directly. The compiler will insert the call for you when the function goes out of scope, or when it's deleted:
1
2
3
4
5
{
    Matrix b(2,1);
    Matrix a(1,2);
    ...
}  // Compiler inserts call to destructor on a and b here. 


In the destructors, delete the arrays for each row first, so you'll do something like delete [] array[i]; inside a loop. Then you delete array itself with delete [] array;
Topic archived. No new replies allowed.