Must I free allocated memory before the program exits?

N

Ning

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);
}
 
M

Mike Wahler

Ning said:
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."

But your program should not depend upon it.
Always clean up after yourself.
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?

Do what you like, but I surely wouldn't do that.
#include <stdlib.h>

int main()
{
int *p;

p = malloc(sizeof *p);

free(p) /* so I can get some sleep :)*/
return(0);
}

-Mike
 
M

Mac

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);
}

This has been discussed here before. It does seem to be true that modern
OS's reclaim all memory on exit. And it does follow from that that you
don't need to free before you exit.

But note that this is not to say that you never need to free. You always
need to free before you loose track of where the malloc()'d block points
to, otherwise you have a memory leak.

So your code above is probably OK, but this code wouldn't be OK:

#include <stdlib.h>
int main (int argc, char **argv)
{
char *p;
int i;

for (i=0; i<argc; i++)
p=malloc(1);
return 0;
}

Since the program loses track of malloc'd blocks, it has a memory leak.
As you can probably imagine, a memory leak in a loop in a program that
runs for a long time could be disastrous.

Mac
--
 
C

Christian Bau

"Ning said:
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);
}

Here's the problem: It is all fine for a small program doing some little
task. But then you incorporate that little task into a bigger program.
And every time your little task is performed you leak a bit of memory
because you don't free pointers. Because the bigger program doesn't
exit, that memory leak is real and when your little task is performed a
few thousand times, suddenly your program crashes.

Better do it right the first time. It is usually much easier and quicker
to do it right the first time than to start with a mess and trying to
clean it up later.
 
D

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top