Well I think I know the answer to the question but I rather be assured. If you malloc memory on the heap and exit the program before you free the space does the os or compiler free the space for you ?
The OS free the space for you when it removes the process's descriptor (task_struct in Linux's case) from its process list.
The compiler usually generates an exit() system call and there's where all this is handled.
On Linux, basically, all these happens in the kernel's exit_mm() function, eventually invoked by exit(). It will release the address space owned by the process with mm_release() function. All that source is available for you to read (although it might get a little complicated :) But yes, the operating system is in charge of releasing process's resources.
And just because I like it so much, if you're into these topics, this is a very GOOD reading: Understanding the Linux Kernel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With