I hope this is the right place to ask this question, I just couldn't find any suitable place. I tried to answer this to myself but couldn't.
Why isn't dynamic memory (allocated on the heap) automatically deallocated by the operating system?
Why is it the process responsibility to do it?
If a program does something stupid like that:
These are 20 bytes that are not gonna be accessible by anyone, not even the parent-process that have created the process running this code.Code:int main() { malloc(20); return 0; }
If it is a shared memory allocation, then there is a solid argument in favor of keeping it persist in memory even after the process creating it has ended, and you can in fact access it later through the OS API (the <sys/shm.h> library, if I remember correctly, provides this functionality), but I can't find any good reason for when it is not a shared memory. And it's not like the OS can't tell the difference between a shared memory allocation and a 'local' memory allocation - since its API provides different functions to do it.