Questions regarding heap deallocate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sean

    Questions regarding heap deallocate

    Here is a confusion about free() and delete. Someone told me that the memory
    chunk allocated in c++ will be automatically released back to system for
    future use. I haven't been convinced.

    In C,
    main()
    {
    int* p
    p = malloc(100*size of(int));
    return 0; //A
    }
    (1) After my program returns, will this chuck of heap be released back to
    system?
    (2) if I change line A to exit(1), will this chuck of heap be released back
    to system?

    In C++
    int main()
    {
    int* p = new int[100];
    return 0; //B
    }

    (3) someone told me that this chuck of heap has been released back to system
    after the program exits, EVEN THOUGH there is no delete operation. Is it
    TRUE or not?
    (4) If it's true: if I change line B to exit(1), will the memory be
    released?

    Thank you


  • Gianni Mariani

    #2
    Re: Questions regarding heap deallocate [OT]


    This question is not addressed by the C++ standard and hence is off
    topic in this NG. Perhaps comp.progrmming would be a better forum.

    I've taken a swag at it for you - see below.

    Sean wrote:[color=blue]
    > Here is a confusion about free() and delete. Someone told me that the memory
    > chunk allocated in c++ will be automatically released back to system for
    > future use. I haven't been convinced.
    >
    > In C,
    > main()
    > {
    > int* p
    > p = malloc(100*size of(int));
    > return 0; //A
    > }
    > (1) After my program returns, will this chuck of heap be released back to
    > system?[/color]

    This depends on your operating system. If you are writing the kernel,
    you can probably bet it will not be released, if you are writing an
    application on Unix/BSD/Linux/AIX/Solaris/HPUX/VMS/VM etc etc is will be
    released and on Win32 it's released most of the time.
    [color=blue]
    > (2) if I change line A to exit(1), will this chuck of heap be released back
    > to system?[/color]

    On Unix/Win32 Reclaiming memory (and other resouces) happens whenever
    the process exits, no matter how it exits. (call to exit or kill signal).
    [color=blue]
    >
    > In C++
    > int main()
    > {
    > int* p = new int[100];
    > return 0; //B
    > }
    >
    > (3) someone told me that this chuck of heap has been released back to system
    > after the program exits, EVEN THOUGH there is no delete operation. Is it
    > TRUE or not?[/color]


    Same exact issue as malloc/free comments to (1) apply here.
    [color=blue]
    > (4) If it's true: if I change line B to exit(1), will the memory be
    > released?[/color]

    Same exact answer to (2) applies here.


    Comment

    • Jack Klein

      #3
      Re: Questions regarding heap deallocate

      On Sun, 21 Sep 2003 17:33:20 -0500, "Sean" <yang22@purdue. edu> wrote
      in comp.lang.c++:
      [color=blue]
      > Here is a confusion about free() and delete. Someone told me that the memory
      > chunk allocated in c++ will be automatically released back to system for
      > future use. I haven't been convinced.[/color]

      I'm glad you told us. We've all been waiting impatiently here to find
      out whether you have been convinced or not. Thank you for ending the
      unbearable suspense.

      And who was it that told you this? Your mail delivery person? Car
      mechanic? Hair stylist? What reason do you have to give any credence
      to that person's pronouncements?
      [color=blue]
      > In C,
      > main()[/color]

      Under the current C standard the above definition of main() is illegal
      and requires a diagnostic. There is no such thing as implicit int
      anymore, every declarator is required to specify a type:

      int main()
      [color=blue]
      > {
      > int* p[/color]

      This is undefined behavior if <stdlib.h> is not included, or a proper
      prototype for malloc() is not in scope.
      [color=blue]
      > p = malloc(100*size of(int));
      > return 0; //A
      > }
      > (1) After my program returns, will this chuck of heap be released back to
      > system?[/color]

      Who knows? The C standard does not and cannot specify what the
      conditions are before a C program is invoked, or after it ends. In
      fact, the C standard does not and cannot specify what any operating
      system does.
      [color=blue]
      > (2) if I change line A to exit(1), will this chuck of heap be released back
      > to system?[/color]

      Same answer.
      [color=blue]
      > In C++
      > int main()
      > {
      > int* p = new int[100];
      > return 0; //B
      > }
      >
      > (3) someone told me that this chuck of heap has been released back to system
      > after the program exits, EVEN THOUGH there is no delete operation. Is it
      > TRUE or not?[/color]

      The C++ standard has no more control over the behavior of any
      operating system than the C standard does.
      [color=blue]
      > (4) If it's true: if I change line B to exit(1), will the memory be
      > released?[/color]

      I have no idea. Consult your compiler and operating system
      combination.
      [color=blue]
      > Thank you[/color]

      Many operating systems automatically reclaim all memory allocated to a
      program when the program ends. Others do not. Neither the C nor C++
      standard is an any position to try and force operating systems to
      operate in any particular manner.

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

      Comment

      Working...