Zahid said:
I am extremely surprised that my C++ compiler does not invoke the
destructor on dynamically created objects upon program termination. As
part of the overall "cleanup" upon program termination I was expecting
the destructors of all the dynamically created objects to get invoked.
That's an unreasonable expectation. Did you also expect that 'free' would
be called for every pointer obtained from 'malloc'? If not, why not?
The "overall cleanup" cannot know the logic behind your allocations. When
the program terminates, the memory is freed by the OS. The OS has _no_
_idea_ about destructors or classes in your program. They are a figment
of your imagination when you write the code. Only the compiler could know
about them when it compiles your code. But the compiler can't guess your
intentions either. If you don't delete the memory, why should the
compiler take over? What if it was your intention _not_ to invoke the
destructor? If invoking the destructor would be some kind of "default"
behaviour, what would you do to prevent it in case you really don't want
it?
Is this a quirk of my compiler, or is this the norm? I know that I am
a "nobody", but this does not make any sense to me at all.
You're probably a Java programmer who is in habit of not cleaning after
himself relying on the garbage collector. Well, here is the in C++
whatever _you_ allocate, _you_ need to deallocate. Any object you obtain
by using 'new' or 'new[]' you need to free by using 'delete' or 'delete[]'
(respectively). And since construction of the object is part of getting
it from free store by the new operator, destruction will only happen when
you deallocate the memory using the delete operator.
What book are you reading that doesn't explain that simple fact?
V