Thread: exit()

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-10-2009, 12:35 AM
  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, 07: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