[SOLVED] Leaking memory

int main()
  for (int i = 0; i < 100; ++i){
    int* ptr = new int;   
  return 0;

My question is:

What happens with memory leaks AFTER the application ends?
Does the system regain dynamically allocated memory leaked during application?
Last edited on
After the application terminates, the system should reclaim its full memory space (regardless of leaks)
Last edited on
Marcos Modenesi wrote:
What happens with memory leaks AFTER the application ends?
Afaik, there isn't anything in the language standard that talks about what happens to dynamically allocated memory after a program exits. That's left up to the operating system. Modern operating systems implement memory managers that will clean up after dirty programs. It's bad practice to rely on that though.
If you're programming for micro controllers, when there is no memory manager, dynamically allocated memory that isn't freed could be tied up permanently until the unit is rebooted.

If you make use of smart pointers, then you will not have to worry about new / delete at all. Prefer the use of std::make_shared rather than new. Read up about them .


There are other types of smart pointers too.

One of the problems with delete is that it may not be called at all because some other thing throws an exception somewhere. Smart pointers mean that the object will always be destructed.

Hope all goes well.
Thank you all for your answers. Very helpfull.
Topic archived. No new replies allowed.