Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens to malloc?

Tags:

c

malloc

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 ?

like image 263
user2861799 Avatar asked Mar 27 '26 13:03

user2861799


1 Answers

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.

like image 64
Paulo Bu Avatar answered Mar 29 '26 04:03

Paulo Bu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!