Suppose I malloc some memory into some pointers but don't free them before the program exits. Does this memory get freed automatically on exit or will the memory leak continue to be there till I restart the computer?
-
1Go through [this](http://stackoverflow.com/questions/2215259/will-malloc-implementations-return-free-ed-memory-back-to-the-system) and [this](http://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc) too. – Suvarna Pattayil Apr 08 '13 at 15:10
6 Answers
The answer is, most often.
Freeing the heap is responsiblity of the OS. While most OS (especially mainstream OS) frees the heap upon exit, it is not necessarily true of say embedded system OS.
When you call for memory to be allocated on the heap, a system call is made to the kernel space of the OS to provide this memory. This memory is mapped to your process structure, which is maintained by the OS. When your program exits, the OS goes through a clean up routing, closing all file descriptors, and marks this memory free for allocation to other processes (among other things).
Some of these answers are incorrect in saying that it is compiler dependant. The compiler does not say 'hey free all this memory on program exit'. That wouldn't make sense, what happens if the OS unexpectedly terminates the program then? No, the compiler is responsible for generating system calls whenever memory allocation/deallocation is explicitly requested for the heap.

- 4,112
- 1
- 21
- 39
-
Thank you, @75inchpianist, for such a great and clear explanation. – Stanley Sathler Apr 08 '20 at 20:46
Memory won't be freed by your program or libc, but will be freed by the operating system on all modern operating systems. They assign memory to specific processes and clean up the memory when the process terminates.

- 10,343
- 1
- 41
- 43
Any modern desktop operating system would reclaim the resources when the process exits. There will be no memory leak.

- 486,780
- 108
- 951
- 1,012
It depends on what OS you are using. Obviously, any modern desktop OS is designed to clear after you when your program exits, if necessary.

- 2,061
- 1
- 19
- 18
If you look at the C standard, it's implementation specific so you can't be sure about that.
But most OS will free the memory once a process is terminated, but it may not be the case on some smaller/simpler platforms.

- 10,508
- 1
- 41
- 52
That is not a c question. How the heap is implemented depends on compiler and what the os does after a program exits. To my knowledge all modern os free memory resources when a program exits. This may not be true on some embedded systems or drivers.

- 25,014
- 6
- 48
- 78
-
how the heap is freed after the program exits is independant from the compiler. it is an OS issue. – 75inchpianist Apr 08 '13 at 15:06
-
Yea thats what I tried to say. but not well. The complier has some say on how heaps are implemented. You can have C programs on Os's that don't have virtual address spaces or memory management. On all modern implantation the compiler forwards the calls to system calls but thats not necessarily the case – rerun Apr 08 '13 at 15:09