Will the allocated memory be released after process exits?

Discussion in 'C Programming' started by lovecreatesbeauty, Oct 24, 2005.

  1. Hello experts,



    When I allocate memory use malloc() and forget to call a corresponding
    free(), does this allocated memory lose forever after program exits
    (Can i regard it same as process exits? )?

    Or will the allocated memory be released after program exits?



    Sincerely,

    lovecreatesbeauty
     
    lovecreatesbeauty, Oct 24, 2005
    #1
    1. Advertising

  2. lovecreatesbeauty wrote:
    >
    > When I allocate memory use malloc() and forget to call a corresponding
    > free(), does this allocated memory lose forever after program exits
    > (Can i regard it same as process exits? )?


    The standard does not specify this.*

    > Or will the allocated memory be released after program exits?


    On modern desktop operating systems, yes. On more specialised
    operating systems, perhaps not.

    You should structure your code so that you can easily deallocate
    any and all dynamic objects. However, many programs avoid actually
    doing the deallocation on program exit because, one some systems,
    it can needlessly cause the system to thrash.

    However, the more complicated your program, the more sense it makes
    to deallocate memory. If system resources are tied to given memory
    allocations, then the deallocation routines in your code can (and
    should) untie/close those system links as the same time.

    So, the choice is yours.

    * Certainly not in any way that a strictly conforming program could
    ever tell.

    --
    Peter
     
    Peter Nilsson, Oct 24, 2005
    #2
    1. Advertising

  3. lovecreatesbeauty wrote:
    > Hello experts,
    >
    >
    >
    > When I allocate memory use malloc() and forget to call a corresponding
    > free(), does this allocated memory lose forever after program exits
    > (Can i regard it same as process exits? )?
    >
    > Or will the allocated memory be released after program exits?


    If you include your code in a try/catch block it will be dealocated by
    garbage collector. But this is implemented only in a few compilers as
    a add-on, like lcc.
    Please ermember that this has nothing to do with the C standard.
    example
    try
    {
    //code here
    }

    catch(/*Exception e*/)
    {
    //do something with the exception or throw it again
    }

    If you know java this will be very easy.
     
    Pacher R. Dragos, Oct 24, 2005
    #3
    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. Replies:
    5
    Views:
    623
    Matt Wharton
    Dec 9, 2004
  2. Ning
    Replies:
    4
    Views:
    705
    Dan Pop
    Sep 26, 2003
  3. Brad Tilley

    See when process exits in Windows XP

    Brad Tilley, Oct 17, 2004, in forum: Python
    Replies:
    3
    Views:
    363
    Brad Tilley
    Oct 17, 2004
  4. Replies:
    30
    Views:
    1,481
    niraj
    Jul 3, 2011
  5. Ryan

    notify when a process exits

    Ryan, Jul 28, 2011, in forum: Python
    Replies:
    0
    Views:
    171
Loading...

Share This Page