Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is it ever necessary to free memory if you are about to call exitanyway?

Reply
Thread Tools

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

 
 
viza
Guest
Posts: n/a
 
      12-12-2007
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 );
}
 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      12-12-2007
In article <(E-Mail Removed)>,
viza <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Tor Rustad
Guest
Posts: n/a
 
      12-12-2007
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 <(E-Mail Removed) | tr i-za-h a-z>
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      12-13-2007
viza <(E-Mail Removed)> 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
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      12-13-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it ever really necessary to log out ? Moke G Computer Support 4 08-14-2006 10:27 PM
Have you ever had your computer crashed at 3am and wished that you had someone to call? lifestylelink Computer Support 10 08-12-2006 03:05 PM
don't you ever, don't you ever, lower yourself, forgetting all your standards Jason ASP .Net Web Controls 0 07-06-2004 12:50 PM
Is a "cast" ever necessary in "return"? LaEisem C Programming 10 10-29-2003 09:05 PM



Advertisments