some queries on freeing memory allocated using malloc

H

Hassan Iqbal

Hi,
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);
for(i=0;i<n;i++) /* i and n are integers */
p= malloc(n*sizeof p);

then in that case is this command sufficient to free all the allocated
memory:

free(p);

or do i need to free it like this:

for(i=0;i<n;i++)
free(p);
free(p);

(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */

int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */

in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.

thanks,
hassan
 
J

Joona I Palaste

Hassan Iqbal said:

This question comes up fairly regularly here.
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);
for(i=0;i<n;i++) /* i and n are integers */
p= malloc(n*sizeof p);

then in that case is this command sufficient to free all the allocated
memory:

No it isn't.
or do i need to free it like this:

for(i=0;i<n;i++)
free(p);
free(p);


Yes you do.
(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */
int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */
in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.

It should be in the code in func1(). If you add the free(p) code in
func2() after the return(p), it will never get executed, resulting in
memory leaks. Code after a return statement is *never* executed, no
matter what code it is, or which function it appears in.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
- The Official Graffitist's Handbook
 
B

Bertrand Mollinier Toublet

Hassan said:
(1)if i do the memory allocation like:
int **p;
p= malloc(n*sizeof p);

We'll give you the benefit of the doubt (for code written instead of
copy-pasted), but the statement above will not exactly do what you expect.

p = malloc(n * sizeof *p);
for(i=0;i<n;i++) /* i and n are integers */
p= malloc(n*sizeof p);


Same here: p = malloc(n * sizeof **p);
for(i=0;i<n;i++)
free(p);
free(p);

That's what you need to free it all, indeed.
in the above <snipped> pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.
You are quite free to free p wherever between the place it is allocated
at, and the place you last use it. The extreme last place where you can
free p is when it goes out of scope, likely when you are going to leave
the block (between the { and the } ) where p has been declared.
 
A

Al Bowers

Hassan said:
Hi,
(1)if i do the memory allocation like:

You are allocating a multidimensional array of type int. You
need to get the sizes correct. Look at the faq question 6.16
located at http://www.eskimo.com/~scs/C-faq/q6.16.html
int **p;
p= malloc(n*sizeof p);

p = malloc(n*sizeof *p); /* or n*sizeof(int *) */
for(i=0;i<n;i++) /* i and n are integers */
p= malloc(n*sizeof p);


p = malloc(n * sizeof **p); /* or n*sizeof(int) */
then in that case is this command sufficient to free all the allocated
memory:

free(p);

This is wrong.
or do i need to free it like this:

for(i=0;i<n;i++)
free(p);
free(p);

This is correct way to free the allocations.
(2)
consider this:
func1(...)
{
...
...
for(i=0;i<n;i++){
p=func2(...) /* p is an integer pointer */
...
...
}
...
}/* end func1 */

int *func2(...)
{
int *P;
p= malloc(n*sizeof p);
...
...
return(p);
}/* end func2 */

in the above pseudo code where should i free the memory which has been
allocated in func2, supposing that n is significantly large. should it
be every time just before the call to func2 or at the end of func1 or
after return(p) in func2 itself.

You are allocating the space in func2 which is returning a pointer
to the allocated space. You will then use this space in func1 for
some purpose. Once you are finished using the space then
deallocate (free) it. Beware for the hazards in func2 should there
be a allocation failure. In the case you will need to recover from
the allocation failure and signal func2 of the failure. This is
usually done by func2 returning NULL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top