In <bl0fo3$pd$> "Ning" <> writes:
>I am really confused on freeing allocated memory before the
>program exits.
>
>7.24 of faq says that "A real operating system definitively
>reclaims all memory when a program exits."
>http://www.eskimo.com/~scs/C-faq/q7.24.html
>So if I am writing a C program intended to be run in a
>"real operating system", does the faq mean that I can write
>code like this and still sleep peacefully in the night?
>
>#include <stdlib.h>
>
>int main()
>{
> int *p;
>
> p = malloc(sizeof *p);
> return(0);
>}
Yes. The big point about free() is that it's not supposed to return
*anything* to the operating system, being it real or not. The memory
is simply made available for further allocation to the program.
There are other arguments, mostly religious, for freeing everything
yourself before program termination, but causing system-wide memory
leaks after program termination is not one of them. The most valid is
that, later, the current main function may be renamed and repeatedly
called by a long running (or never terminating) program. And if you
forget to add the necessary clean-up code...
This scenario happended to me exactly once, but I didn't forget to add
the clean up code. Therefore, I'm not inclined to worry about this
(more hypothetical than real) possibility, especially considering that
free() is often one of the most expensive (in terms of CPU cycles)
functions from the standard library.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: