Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   is it ever necessary to free memory if you are about to call exitanyway? (http://www.velocityreviews.com/forums/t560520-is-it-ever-necessary-to-free-memory-if-you-are-about-to-call-exitanyway.html)

viza 12-12-2007 03:41 PM

is it ever necessary to free memory if you are about to call exitanyway?
 
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 );
}

Richard Tobin 12-12-2007 03:58 PM

Re: is it ever necessary to free memory if you are about to call exitanyway?
 
In article <23344bb7-129a-4ded-b7a9-6bf885bf5625@e23g2000prf.googlegroups.com>,
viza <tom.viza@gmail.com> wrote:

>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
--
:wq

Tor Rustad 12-12-2007 10:54 PM

Re: is it ever necessary to free memory if you are about to callexit anyway?
 
viza wrote:
> 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).

--
Tor <bwzcab@wvtqvm.vw | tr i-za-h a-z>

Peter Nilsson 12-13-2007 05:08 AM

Re: is it ever necessary to free memory if you are about to call exitanyway?
 
viza <tom.v...@gmail.com> wrote:
> 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.

--
Peter

Flash Gordon 12-13-2007 08:35 AM

Re: is it ever necessary to free memory if you are about to callexit anyway?
 
Peter Nilsson wrote, On 13/12/07 05:08:

<snip discussion on whether or not to free memory before exiting>

> 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.
--
Flash Gordon


All times are GMT. The time now is 05:57 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.