Hi,
I'm wondering what the exit() does in C? does it release dynamic allocation made by the program?
What arguments come with it?
Hi,
I'm wondering what the exit() does in C? does it release dynamic allocation made by the program?
What arguments come with it?
According to the C standard:
Originally Posted by C Standard, 1999 Edition, Section 7.20.4.3
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
No, at least not according to the standard's description.Originally Posted by msshapira
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.Originally Posted by msshapira
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.