views:

244

answers:

5

Possible Duplicate:
When you exit a C application, is the malloc-ed memory automatically freed?

In C, is it necessary to free a pointer at exit? When the program exists, does it free memory from pointers still pointing to an allocated block?

Is it dependent on the OS?

+6  A: 

The OS will free anything allocated by your program when the program exits. However, it is good practice to always free what you've allocated.

Jeff Barger
A: 

It depends on the OS. Although in most cases, forgetting to free the pointer won't do too much harm, doing so is not a good idea either. This can lead into memory leak. If happening long enough, you can experience a strange shortage of memory that you can't access, causing other programs to not having enough memory to operate.

If you're talking about explicitly freeing a dynamically allocated array that a pointer is pointing to, in short, yes, you do have to go through that process. When the program exits, if your program did not free that memory block, the pointer is still pointing to the allocated block, but no longer allows you to have access to it since the program is out of scope upon exit.

stanigator
Necessary? No.Good Practice/Habbit? Yes.
WarmWaffles
-1: after program exit?
John Saunders
@John: What part are you talking about?
stanigator
@stanigator: "when the program exits, if your program did not free that memory block, the pointer is still pointing to the allocated block"
John Saunders
oh...what I should've said is that the pointer is now undefined after program exits. I will remove my answer a while later.
stanigator
+5  A: 

From what I know, for the most part, the OS will free any process memory when the process terminates, at least under most common user OSes (Windows, Linux, etc). The OS will also perform cleanup if the process crashes or such.

HOWEVER, relying on the OS to perform cleanup is not proper coding procedure and you can't always guarantee it'll do what you want. You should always perform your own garbage collection if you want it done right and at the right time (I've had programs crash during exit because the system cleaned up memory in an odd order and created some invalid pointers, that it then tried to free).

Process memory cleanup may only apply to memory allocated by your original process or thread. If you spawn new processes, these could keep executing. If you use an already-running service and call some method that allocates memory then gives you control, that may not get cleanup up.

Some video drivers won't free VRAM immediately and on some older cards, running a process that leaked VRAM repeatedly would eventually crash your system.

You should always free any memory you allocate, especially if your process may restart or keep executing.

peachykeen
there are lots of times that it makes sense to let the OS clean things up, mostly because the lifetime is not clear (EG shared dynamic data structures in a library, it's not clear when it should be free'd, see http://0pointer.de/blog/projects/beware-of-xmlCleanupParser for an example of when this sort of thing causes problems)
Spudd86
For the most part, letting the OS do it works, but it's still not really right; you should clean up your own memory. In odd examples of code (like what you linked), doing it yourself and not doing it yourself can both lead to crashes. So whether you have to do it depends on the situation, but generally you should.
peachykeen
+6  A: 

It's not strictly necessary, in fact it can sometimes be convenient to "leak" these pointers to avoid worrying about ordering of destructors.

No modern OS is going to leak this memory, all the memory used by the process will be reclaimed.

Stephen
Even DOS wouldn't leak in this situation... and it didn't even have virtual memory... pretty much ANY OS that actually has a concept of 'process' and 'exit' should take care of this...
Spudd86
@spudd86 : I would consider DOS a modern OS :) ... just not _that_ modern!
Stephen
+2  A: 

This is OS-dependent, but really any modern operating system should take back all of your resources when you terminate.

That said, it's really a good idea to free any memory you acquire if you can -- and in all but few cases you can. Also, remember we're talking about memory here, not resources in general. There are other things you may wish to do in a proper cleanup; things the OS can't guess and can't do for you. For example, disconnect from some external service, or delete some temporary file.

wilhelmtell