Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C, is it necessary to free a pointer at exit? [duplicate]

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?

like image 953
Weboide Avatar asked Nov 29 '22 05:11

Weboide


1 Answers

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.

like image 169
ssube Avatar answered Dec 04 '22 22:12

ssube