Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Must I free allocated memory before the program exits?

Reply
Thread Tools

Must I free allocated memory before the program exits?

 
 
Ning
Guest
Posts: n/a
 
      09-26-2003
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);
}


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-26-2003

"Ning" <> wrote in message
news:bl0fo3$pd$...
> 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


 
Reply With Quote
 
 
 
 
Mac
Guest
Posts: n/a
 
      09-26-2003
On Thu, 25 Sep 2003 23:36:04 +0000, Ning wrote:

> 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
--
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      09-26-2003
In article <bl0fo3$pd$>,
"Ning" <> wrote:

> 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.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      09-26-2003
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:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What standard library functions must deal with allocated memory? Francois Grieu C Programming 12 04-28-2012 11:09 AM
will the memory allocated by malloc get released when program exits? keredil@gmail.com C Programming 30 07-03-2011 05:09 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Free Allocated Memory Error (delete) alan C++ 1 03-04-2004 02:30 AM
Free Allocated Memory Error (delete) alan C++ 3 02-18-2004 03:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57