### Thread: Memory allocation inside of a function that returns double pointer

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
2
Rep Power
0

#### Memory allocation inside of a function that returns double pointer

I would like to know if there is any problem with
not freeing the memory I allocate inside a function that returns a double pointer
Example:

void main()
{

int **transpose(int **a,int n,int m);

int **b;

mat=imatrix(0,3,0,4); /*this calls a function that allocates memory for the array of integers */

mat[0][0] = 0;
mat[0][1] = 0;
mat[0][2] = 0;
mat[0][3] = 0;
mat[1][0] = 1;
mat[1][1] = 2;
mat[1][2] = 3;
mat[1][3] = 4;
mat[2][0] = 5;
mat[2][1] = 6;
mat[2][2] = 7;
mat[2][3] = 8;

b=imatrix(0,4,0,3);
b=transpose(mat,3,4);

free_imatrix(b,0,4,0,3); /* free the allocated memory inside the main() */
}

int **transpose(int **a,int n,int m) {
/*function that takes a[0,..,n][0,...,m] as input and gives its transpose as output */
int i,j;
int **b;

b=imatrix(0,m,0,n); /*allocate memory for the array of integers with indices [0,..,m][0,...n]*/

for (i=0;i<n;i++)
for (j=0;j<m;j++)
b[j][i]=a[i][j];
return b;

}

The question is. What happens if I do not free the memory allocated for variable b inside the function "transpose".

It runs free of errors like this but I am concerned when I have to use it many times andf with large matrices.
2. > void main()
main returns an int

> What happens if I do not free the memory allocated for variable b inside the function "transpose".
You don't free the matrix created within imatrix either.
Actually, nothing happens, so long as you keep a pointer to the memory alive.

Given the code you've posted, you would finish with say
Code:
```free_imatrix(b,0,4,0,3); /* free the allocated memory inside the main() */
free_imatrix(mat,0,3,0,4);```
So long as your imatrix() and free_imatrix() calls match up, I don't see any problems with memory leaks here.
3. On the one hand, it isn't necessary to free memory allocated in a given function, provided that the memory is freed somewhere in the program run.

OTOH, it is better to localize the allocation and deallocation of memory as much as possible, to limit the chances of a memory leak. If the memory is used only within the function, then you should free it within that function; similarly, if you are replacing a pointed-to block with a newly allocated one, then you should make sure that all pointers to the original are updated, then free the original block once you have finished with it. Conversely, you don't want to free memory that might be aliased to more than one pointer.

In the case in question, the problem is that you are assigning a newly-allocated value to the variable b in main() (poor naming, BTW - having two variables with the same name in overlapping scopes can be confusing), then immediately overwriting the pointer without freeing the memory it points to. The line

Code:
`b=imatrix(0,4,0,3);`
is not merely unnecessary, it introduces a memory leak that is completely avoidable.

Also, as Salem points out, you will want to deallocate both of the final values at the end of the program. While most systems will automatically free all allocated memory at the close of a program, relying on this is extremely poor practice; never leave allocated memory unfreed at the end of the program.
Last edited by Schol-R-LEA; March 17th, 2006 at 08:37 PM.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
2
Rep Power
0
I would like to thank you both for taking the time.
What you said it was more than enough to answer my question.

Codrut