Will using delete with a base class pointer cause a memory leak?
c++, memory-leaks, memory-management
Solution
Unless the base class destructor is virtual, it's undefined behaviour. See 5.3.5/4:
If the static type of the operand [of the delete operator] is different from its dynamic type, the static type shall be a base class of the operand's dynamic type and the static type shall have a virtual destructor or the behaviour is undefined.
Problem
Given two classes have only primitive data type and no custom destructor/deallocator. Does C++ spec guarantee it will deallocate with correct size? ``` struct A { int foo; }; struct B: public A { int bar[100000]; }; A *a = (A*)new B; delete a; ``` I want to know do I need to write an empty `virtual` dtor? I have tried g++ and vc++2008 and they won't cause a leak. But I would like to know what is correct in C++ standard.