In my C++ main
function, for example, if I had a pointer to a variable which uses heap memory (as opposed to stack memory) - is this automatically deallocated after my application exits? I would assume so.
Even so, is it good practice to always delete heap allocations even if you think they will never be used in a situation where the memory is automatically deallocated on exit?
For example, is there any point in doing this?
int main(...)
{
A a = new A();
a->DoSomething();
delete a;
return 0;
}
I was thinking maybe in case I refactor (or someone else refactors) that code and puts it elsewhere in the application, where delete
is really neccecary.
Edit:
As well as the answer by Brian R. Bondy (which talks specifically about the implications in C++), Paul Tomblin also has a good answer to a C specific question, which also talks about the C++ destructor.