0

Hi!, im new in this forum (and programming in c :)) (and sorry, my english is not good enough, I speak spanish)
I have a problem trying to release memory from a Matrix structure.
When I try to release a square matrix there is no problem, i can do it, if I try to realease a Matrix with more columns than rows i have not problem, but if i have more rows than columns my application failed.
Here is my code:
For the creation of the matrix (a matrix of 1s):

Mat* newmatrix(int a,int b)
{
	Mat *matri=(Mat*)malloc(sizeof(Mat));
	matri->row=a;
	matri->co=b;
	matri->data=(int**)malloc(a*sizeof(int*));
	int j;
	for ( j=0 ; j<a ; j++ )
	{
		matri->data[j] = (int*)malloc(b*sizeof(int)); 
	}
	int x,y;
	for ( x=0; x<a;x++)
	{
		for (y=0;y<a;y++)
		{
			mat_set(matri,x,y,1);
		}
	}
	return matri;
}

For matrix release of memory:

void freemat (Mat* matrix)
{
   int j;

   for (j=0;j<(m->row);j++)
   {
	free(matrix->data[j]); 
   }
   free(matrix->data);
   free(matrix);
}

I will really appreciate your help, Thank you!!
Carlos.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Martin B
0

The problem is in line 15.
You are filling the matrix as if it is an a * a matrix, but it is an a * b matrix.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.