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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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.

Thank you in advance.
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.