Must I free allocated memory before the program exits?

Discussion in 'C Programming' started by Ning, Sep 26, 2003.

  1. Ning

    Ning Guest

    I am really confused on freeing allocated memory before the
    program exits.

    7.24 of faq says that "A real operating system definitively
    reclaims all memory when a program exits."
    http://www.eskimo.com/~scs/C-faq/q7.24.html
    So if I am writing a C program intended to be run in a
    "real operating system", does the faq mean that I can write
    code like this and still sleep peacefully in the night?


    #include <stdlib.h>

    int main()
    {
    int *p;

    p = malloc(sizeof *p);
    return(0);
    }
     
    Ning, Sep 26, 2003
    #1
    1. Advertising

  2. Ning

    Mike Wahler Guest

    "Ning" <> wrote in message
    news:bl0fo3$pd$...
    > I am really confused on freeing allocated memory before the
    > program exits.
    >
    > 7.24 of faq says that "A real operating system definitively
    > reclaims all memory when a program exits."


    But your program should not depend upon it.
    Always clean up after yourself.

    > http://www.eskimo.com/~scs/C-faq/q7.24.html
    > So if I am writing a C program intended to be run in a
    > "real operating system", does the faq mean that I can write
    > code like this and still sleep peacefully in the night?


    Do what you like, but I surely wouldn't do that.

    >
    >
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int *p;
    >
    > p = malloc(sizeof *p);


    free(p) /* so I can get some sleep :)*/

    > return(0);
    > }


    -Mike
     
    Mike Wahler, Sep 26, 2003
    #2
    1. Advertising

  3. Ning

    Mac Guest

    On Thu, 25 Sep 2003 23:36:04 +0000, Ning wrote:

    > I am really confused on freeing allocated memory before the
    > program exits.
    >
    > 7.24 of faq says that "A real operating system definitively
    > reclaims all memory when a program exits."
    > http://www.eskimo.com/~scs/C-faq/q7.24.html
    > So if I am writing a C program intended to be run in a
    > "real operating system", does the faq mean that I can write
    > code like this and still sleep peacefully in the night?
    >
    >
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int *p;
    >
    > p = malloc(sizeof *p);
    > return(0);
    > }


    This has been discussed here before. It does seem to be true that modern
    OS's reclaim all memory on exit. And it does follow from that that you
    don't need to free before you exit.

    But note that this is not to say that you never need to free. You always
    need to free before you loose track of where the malloc()'d block points
    to, otherwise you have a memory leak.

    So your code above is probably OK, but this code wouldn't be OK:

    #include <stdlib.h>
    int main (int argc, char **argv)
    {
    char *p;
    int i;

    for (i=0; i<argc; i++)
    p=malloc(1);
    return 0;
    }

    Since the program loses track of malloc'd blocks, it has a memory leak.
    As you can probably imagine, a memory leak in a loop in a program that
    runs for a long time could be disastrous.

    Mac
    --
     
    Mac, Sep 26, 2003
    #3
  4. In article <bl0fo3$pd$>,
    "Ning" <> wrote:

    > I am really confused on freeing allocated memory before the
    > program exits.
    >
    > 7.24 of faq says that "A real operating system definitively
    > reclaims all memory when a program exits."
    > http://www.eskimo.com/~scs/C-faq/q7.24.html
    > So if I am writing a C program intended to be run in a
    > "real operating system", does the faq mean that I can write
    > code like this and still sleep peacefully in the night?
    >
    >
    > #include <stdlib.h>
    >
    > int main()
    > {
    > int *p;
    >
    > p = malloc(sizeof *p);
    > return(0);
    > }


    Here's the problem: It is all fine for a small program doing some little
    task. But then you incorporate that little task into a bigger program.
    And every time your little task is performed you leak a bit of memory
    because you don't free pointers. Because the bigger program doesn't
    exit, that memory leak is real and when your little task is performed a
    few thousand times, suddenly your program crashes.

    Better do it right the first time. It is usually much easier and quicker
    to do it right the first time than to start with a mess and trying to
    clean it up later.
     
    Christian Bau, Sep 26, 2003
    #4
  5. Ning

    Dan Pop Guest

    In <bl0fo3$pd$> "Ning" <> writes:

    >I am really confused on freeing allocated memory before the
    >program exits.
    >
    >7.24 of faq says that "A real operating system definitively
    >reclaims all memory when a program exits."
    >http://www.eskimo.com/~scs/C-faq/q7.24.html
    >So if I am writing a C program intended to be run in a
    >"real operating system", does the faq mean that I can write
    >code like this and still sleep peacefully in the night?
    >
    >#include <stdlib.h>
    >
    >int main()
    >{
    > int *p;
    >
    > p = malloc(sizeof *p);
    > return(0);
    >}


    Yes. The big point about free() is that it's not supposed to return
    *anything* to the operating system, being it real or not. The memory
    is simply made available for further allocation to the program.

    There are other arguments, mostly religious, for freeing everything
    yourself before program termination, but causing system-wide memory
    leaks after program termination is not one of them. The most valid is
    that, later, the current main function may be renamed and repeatedly
    called by a long running (or never terminating) program. And if you
    forget to add the necessary clean-up code...

    This scenario happended to me exactly once, but I didn't forget to add
    the clean up code. Therefore, I'm not inclined to worry about this
    (more hypothetical than real) possibility, especially considering that
    free() is often one of the most expensive (in terms of CPU cycles)
    functions from the standard library.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Sep 26, 2003
    #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. alan
    Replies:
    3
    Views:
    2,791
    Sharad Kala
    Feb 18, 2004
  2. Replies:
    5
    Views:
    662
    Matt Wharton
    Dec 9, 2004
  3. Replies:
    30
    Views:
    1,509
    niraj
    Jul 3, 2011
  4. lovecreatesbeauty

    Will the allocated memory be released after process exits?

    lovecreatesbeauty, Oct 24, 2005, in forum: C Programming
    Replies:
    2
    Views:
    326
    Pacher R. Dragos
    Oct 24, 2005
  5. Francois Grieu
    Replies:
    12
    Views:
    847
    Malcolm McLean
    Apr 28, 2012
Loading...

Share This Page