### Problems with dynamic allocation

Hi everybody ... i'm now working on a class to handle matrices and matrix operations. I dynamically allocate a two dimensional array with its size as an input. My problem is that i need to resize the matrix structure (to fit to the size of a product of two matrices or to adjust the size after deleting a row or column for example). I make the resizing by deleting the allocated 2d array after saving its first element address to another pointer to pointer, after saving the useful data in an other array, then i use new[] operator to create a new 2d array using the same address of the old deleted array.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````void My_matrix ::delete_row(int a) { int i,j; int buffer_row_index = -1; //note that our work is zero based, as we acess the private member of an object of the same class we are working on and we access to the array as it is (zero based) so if this variable is -1, it will be 0 when it enters for the first time and saves the data in the right position. My_matrix buffer(number_of_rows-1,number_of_columns); //copying the valid data from the original array to the buffer one for(i = 0; i < number_of_rows; i++) { if (i != (a-1)) { buffer_row_index++; for(j = 0; j < number_of_columns; j++) { buffer.M[buffer_row_index][j] = M[i][j]; } } } //deleting the old M array and allocating a new one with appropriate size for (i = 0; i < original_num_of_rows; i++) { //delete[]M[i]; free(M[i]); } //delete[]M; free(M); M = back_up_pointer; //back_up_pointer saves the old address of M and i use it for re-allocation //re-allocating the array number_of_rows--; original_num_of_rows = number_of_rows; //we have to re set the originals because M has been deleted and reformed original_num_of_cols = number_of_columns; M = new float*[original_num_of_rows]; for (i = 0; i < number_of_rows; i++) { M[i] = new float[number_of_columns]; } for (i = 0; i < number_of_rows; i++) { for (j = 0; j < number_of_columns; j++) { M[i][j] = buffer.M[i][j]; } } }``````

I do this in a way that i think is unsafe. This code is sometimes unstable and gives an unhandled exception whose reason is unknown. I need to know whether this way is good enough or if there any other better ways.

do not mix new/free. Either malloc/free or new/delete. Everything else leads to a crash
i just thought about giving it a try, but when i use new[]/delete[] i still have the same problem of unhandled exceptions.
I feel that new/delete has better syntax than malloc/free.

I have a feeling you are allocating and deallocating improperly, if you are getting exceptions.
Last edited on
let me pose a clearer question... if i use delete[]M to free the memory allocated, then can i use M = new float*[size] in the same code?? is M deleted from memory or just the allocated space is freed and M is nulled??
You can re-use variables as much as you want, though careful not to get confused.

So the direct answer is, only the memory pointed to is marked as free - just like in real life, memory is not created or destroyed - it is only allocated or deallocated.
Topic archived. No new replies allowed.