exit()

This is a discussion on exit() within the C Programming forums, part of the General Programming Boards category; Hi, I'm wondering what the exit() does in C? does it release dynamic allocation made by the program? What arguments ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    exit()

    Hi,
    I'm wondering what the exit() does in C? does it release dynamic allocation made by the program?
    What arguments come with it?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    According to the C standard:
    Quote Originally Posted by C Standard, 1999 Edition, Section 7.20.4.3
    Code:
    #include <stdlib.h>
    void exit(int status);
    The exit function causes normal program termination to occur. If more than one call to the exit function is executed by a program, the behavior is undefined.

    First, all functions registered by the atexit function are called, in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered. If, during the call to any such function, a call to the longjmp function is made that would terminate the call to the registered function, the behavior is undefined.

    Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.

    Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    43
    i also read that.. it's that i dont understand.
    Does it release allocations or not? is there a function that can release all allocation (in case of allocation failure) without releasing alloc by alloc?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    Quote Originally Posted by msshapira
    Does it release allocations or not?
    No, at least not according to the standard's description.

    Quote Originally Posted by msshapira
    is there a function that can release all allocation (in case of allocation failure) without releasing alloc by alloc?
    Generally, the operating system would do that for you anyway. It is not good practice to rely on that (and not all operating systems clean up after processes in that way), so you should still keep track of what you allocate and free the memory as soon as it is not needed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Do you have any examples of which OS's don't clean up memory after a program ends?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Do you have any examples of which OS's don't clean up memory after a program ends?
    I was thinking the same thing.

    All OS's I've ever worked with that allow tasks/processes to end (without issuing some sort of "panic"), does clean up the memory allocated. I'm sure there are micro-OS's that have "no memory cleanup", but I also expect those do not use "exit" to end a process.

    I'm not by any means saying you shouldn't make a best effort of cleaning up memory allocations in the code - for long-running applications, it will not work to "stick you head in the sand and hope for the best", as any memory leaks will accumulate and eventually cause the system to fall over.

    For simple, short-running, applications, it is possibly permissable to explicitly NOT free memory.

    Note that the freeing of memory in the relevant cases is done by the OS, not by the C runtime library. And it is done at a much coarser level than the C runtime library.

    Also note that where I work, we make OS's for Mobile Phones, and we have specific testing to ensure that an application NEVER leaks memory - yes, it gets cleaned up if the app exits. The issue however, is that in embedded systems, there may not be a whole lot of free memory, and even a small amount of leaked memory from time to time can cause undesirable "failure to do the expected operation".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chat Program Help?
    By Paul22000 in forum Networking/Device Communication
    Replies: 9
    Last Post: 02-09-2009, 11:35 PM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. exit() ?
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 04:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21