Nuno Emanuel F. Carvalho wrote:
> > In this case, it doesn't matter. All memory will be freed when the
> > program terminates. In general, an application should free() the data
> > once it has finished with it.
>
> So, foo() function could be called several times that the memory will be
> freed when the program terminates !?
All memory is freed when the program terminates.
> Conclusion: it's only needed to free() memory when the function doesn't
> return the memory allocation !?
It is necessary to call free() if you wish to re-use the memory from
within the same process. E.g. a long-running daemon needs to free
memory when it is no longer used or else it will eventually consume
all available memory.
In general, programs should free memory once they are no longer using
it, as this allows them to re-use that memory. Otherwise, subsequent
calls to malloc() will result in the application being allocated more
memory.
> example:
>
> int foo()
> {
> int a ;
> char *b ;
>
> b = malloc(3*sizeof(char)) ;
> .
> . . .
> .
> free(b) ;
>
> a = 2 ;
> return a ;
> }
>
> Only in this cases it will be needed to free() memory !?
No. There is nothing to gain from calling free() if the program will
terminate shortly, and won't attempt to malloc() any more memory in
the meantime.
--
Glynn Clements <[EMAIL PROTECTED]>