does dynamic memory free up when application ends?

Dynamic memory using new.
I know they need to be deleted while program runs, but if the program exits or main returns, do they get deleted?
// My guess is yes, although unsure about the returning main thing. I'm pretty sure things are usually fine after a program ends, or else I'd be quite bothered that I was never warned of this in the C++ books I read!
Last edited on
Yes, modern OSes will do that for you.
I figured as much, but then that raises the question of the use of hunting for memory leaks of a program.

Other than to be safe in regards to older OS's.

Obviously if you're running a memory-hungry program, you want to use as little as possible at any given time, but why bother checking to see that you're released all the memory at the end of run-time?
Yes, modern OSes will do that for you.

Thanks jsmith, I thought it was true but was having trouble finding a confirmation.
Obviously if you're running a memory-hungry program, you want to use as little as possible at any given time, but why bother checking to see that you're released all the memory at the end of run-time?

Because if there is any chance that it is NOT released by the program itself at the end of the program, that usually means that you have poor code that is full of memory leaks. Memory leaks can bring down any system, no matter how much RAM it has.

Explicitly exempting some object structures from being deleted at program termination can be a design choice for speeding up program termination. But it shouldn't ever be the result of sloppy coding.
Topic archived. No new replies allowed.