C++ heap allocation question
-
Maybe a dumb question, but I was just curious -- are heap-allocated objects in C++ destroyed when a program is exited even if they aren't explicitly freed with calls to delete or free? Thanks!
KR
Yes - all the memory your process used is returned to the system. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Maybe a dumb question, but I was just curious -- are heap-allocated objects in C++ destroyed when a program is exited even if they aren't explicitly freed with calls to delete or free? Thanks!
KR
To be more specific; the heap is destroyed, but the destructor is not called for objects. A non-issue the vast majority of the time (handles will also be closed) but there may be a rare circumstance where a resource needs to be closed a more complex way. (This is rare enough that I can't even think of a non-contrived example.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
To be more specific; the heap is destroyed, but the destructor is not called for objects. A non-issue the vast majority of the time (handles will also be closed) but there may be a rare circumstance where a resource needs to be closed a more complex way. (This is rare enough that I can't even think of a non-contrived example.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Joe Woodbury wrote:
o be more specific; the heap is destroyed, but the destructor is not called for objects.
Good point! Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
To be more specific; the heap is destroyed, but the destructor is not called for objects. A non-issue the vast majority of the time (handles will also be closed) but there may be a rare circumstance where a resource needs to be closed a more complex way. (This is rare enough that I can't even think of a non-contrived example.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Actually, a non-contrived answer was staring me in the face: a file class that caches writes.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke