1
$\begingroup$

I have been trying to manipulate pointers just for the sake of studying. I have had some problems with some programs I wrote where I had memory leak.

Does this memory stay allocated even when the program returns?


  • If yes how to deallocate it?
  • If no why, isn't all saved in heap so only the one who has that heap pointer can deallocate? Or does the operating system pull some magic tricks?

$\endgroup$
2
$\begingroup$

When you run your program its memory allocations take the form of page allocation requests to the operating system. When your program terminates, the operating system notices and returns all the allocated pages to the list of free pages, which can then be used by other processes. It does not matter if your program leaks memory (other than potentially using more memory as a result), because ultimately the operating system will reclaim everything.

$\endgroup$
  • $\begingroup$ Thank you very much. I would want to know something. Is the OS using frame pointers to keep track of the processes' used memory and return that memory where frame pointer points to? $\endgroup$ – anon Mar 11 '16 at 20:40
  • $\begingroup$ @ErinAvllazagaj no, frame pointers are to do with the stack. $\endgroup$ – hobbs Mar 11 '16 at 20:45
  • 1
    $\begingroup$ Note that this is the case on today's desktop/server/mobile/… operating systems, but some low-end embedded devices work differently. @hobbs It's a two-level system. The OS maintains tables of which program owns which bit of memory, and doesn't care what the program uses it for. When the program wants more memory, it makes a system call and the operating system maintains its own table. No matter how much the program messes up, the OS's tables show who owns what. $\endgroup$ – Gilles 'SO- stop being evil' Mar 11 '16 at 20:54
  • $\begingroup$ @Gilles Thanks for the answer. I get it now! The OS pulls out that trick! :) $\endgroup$ – anon Mar 11 '16 at 20:56
  • $\begingroup$ @Gilles yes, absolutely, but frame pointers don't relate to either of those levels :) $\endgroup$ – hobbs Mar 11 '16 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.