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?
Does the memory get unallocated when program exits?
memory management
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.