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

Discussion in 'C Programming' started by viza, Dec 12, 2007.

  1. viza

    viza Guest

    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 );
    }
    viza, Dec 12, 2007
    #1
    1. Advertising

  2. In article <>,
    viza <> 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
    Richard Tobin, Dec 12, 2007
    #2
    1. Advertising

  3. viza

    Tor Rustad Guest

    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 < | tr i-za-h a-z>
    Tor Rustad, Dec 12, 2007
    #3
  4. 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,


    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
    Peter Nilsson, Dec 13, 2007
    #4
  5. viza

    Flash Gordon Guest

    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
    Flash Gordon, Dec 13, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rokia
    Replies:
    4
    Views:
    2,190
    Gernot Frisch
    Jul 26, 2004
  2. LaEisem

    Is a "cast" ever necessary in "return"?

    LaEisem, Oct 28, 2003, in forum: C Programming
    Replies:
    10
    Views:
    624
  3. Panduranga Chary

    How memory function free() knows how much memory to free.

    Panduranga Chary, Dec 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    417
    Keith Thompson
    Dec 27, 2007
  4. iq
    Replies:
    0
    Views:
    219
  5. Jason
    Replies:
    0
    Views:
    197
    Jason
    Jul 6, 2004
Loading...

Share This Page