is it ever necessary to free memory if you are about to call exitanyway?

V

viza

Hi all,

Suppose I have a piece of malloc'd memory that I need for the lifetime
of my program. Is there any reason to free it, or is it guaranteed to
be returned to the system on program termination?

(Theoretical and in practice responses welcome - I am developing for
GNU/Linux, but portability information is also useful. TIA! )

My question is basically, is the any difference between the following
two programs:

program1.c:

int main( void ){
void *ptr= malloc( SOME_SIZE );

whatever_operations( ptr );

exit( EXIT_SUCCESS );
}

program2.c:

void *global;

void freeer( void ){
free( global );
}

int main( void ){
void *ptr= malloc( SOME_SIZE );

global= ptr;
atexit( freeer );

whatever_operations( ptr );

exit( EXIT_SUCCESS );
}
 
R

Richard Tobin

viza said:
Suppose I have a piece of malloc'd memory that I need for the lifetime
of my program. Is there any reason to free it, or is it guaranteed to
be returned to the system on program termination?

It's not guaranteed, but you can rely on it for any common
general-purpose operating system. The sort of case where you can't
rely on it is in embedded systems, but you probably have lots of other
constraints there.

If your code is likely to be re-used in programs where it will be
called repeatedly, obviously it's helpful to free all memory
explicitly.

-- Richard
 
T

Tor Rustad

viza said:
Hi all,

Suppose I have a piece of malloc'd memory that I need for the lifetime
of my program. Is there any reason to free it, or is it guaranteed to
be returned to the system on program termination?

(Theoretical and in practice responses welcome - I am developing for
GNU/Linux, but portability information is also useful. TIA! )

This is really a QoI issue, but I can't think of a hosted environment
that will not do proper clean-up of malloc'ed memory on program termination.

IMO, the main reason for doing free() anyway, would be to please those
doing code review, and ease debugging after memory leaks (i.e. remove
some false positives).
 
P

Peter Nilsson

viza said:
Hi all,

Suppose I have a piece of malloc'd memory that I need for
the lifetime of my program. Is there any reason to free
it,

Since it won't affect the output of a strictly conforming
program, no, there's no requirement to free the memory.
or is it guaranteed to be returned to the system on
program termination?

There are no guarantees on what the host will do behind
the scenes on program termination. There's no guarantee
the implementation will return memory to the system if
you free it anyway (even though it's notionally supposed
to.)

Note that on some older platforms, freeing memory on
program termination was discouraged because it took
ages for some programs to terminate.

These days, that's not really an issue. Indeed, if your
code has the chance of ever being reused, you should at
least supply an option for appropriate deallocation.
 
F

Flash Gordon

Peter Nilsson wrote, On 13/12/07 05:08:

Note that on some older platforms, freeing memory on
program termination was discouraged because it took
ages for some programs to terminate.

On other older platforms it was encouraged because if you did not free
it then it remained allocated even after your program terminated.
Sometimes you just can't win ;-)
These days, that's not really an issue. Indeed, if your
code has the chance of ever being reused, you should at
least supply an option for appropriate deallocation.

Agreed. I agreed with the rest that I snipped as well.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top