BaseClass * p = new DerivedClass();
delete p;
I know the 2nd line will call the destructor of the base class if it doesn't have a virtual destructor and that of the derived class if it does but will delete
properly free the memory (let's say BaseClass
's object takes up 8 bytes of space and DerivedClass
's one 12 - will it free 8 or 12 bytes) and get rid of the object in either case?
Well in the case that it has a virtual
destructor, of course the object will be destroyed and the memory deallocated as expected. If it doesn't have a virtual
destructor, the behaviour is undefined.
if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.
So it doesn't really make any sense to attempt to reason about whether the memory will be fully deallocated or not. The program can do whatever it likes with the memory.