Дана 24/09/23 08:20AM, Chris Bennett написа: > So if I run small program A that has some leaks. I don't free a few > things as suggested. Then I run program B that happens to use those > memory addresses that didn't get freed by program A, what happens then? > Are there problems? Or does it just get used properly?
Programs run in separate memory spaces, they cannot access each other's memory directly (shm_open(3) is another topic). When program A terminates in any way, its entire memory space is freed by the OS, so the deallocation upon exit is redundant. If the termination was due to failure of malloc itself or related to memory management, cleanup deallocation can have unpredictable results and hinder corefile forensics, so is detrimental. Not freeing memory when it stops being needed in a long-running program is another matter, and the case where free(3) should be mandatory. What's highly inconvenient in the matter is the fact that memory leak detection tools cannot distinguish between the two cases, so it's the programmer's job to tell whether a leak in the report is the "real" one or not.