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.
For only primitive data I believe you're fine. You might legitimately not want to incur the cost of a v-table in this case. Otherwise, a virtual d'tor is definitely preferred.
According to the C++ standard, what you have is undefined behaviour - this may manifest itself as a leak, it may not, For your code to be correct you need a virtual destructor.
Also, you do not need that (A*) cast. Whenever you find yourself using a C-style cast in C++, you can be fairly sure that either it is unecessary, or your code is wrong.
This is undefined behaviour - maybe everything's fine, maybe whetever goes wrong. Either don't do it or supply the base class with a virtual destructor.
In most implementations this will not leak - there're no heap-allocated member functions in the class, so the only thing needed when
delete
is done is to deallocate memory. Deallocating memory uses only the address of the object, nothing more, the heap does all the rest.It will deallocate with correct size, because the size to be deallocated is a property of the heap memory region you obtained (there is no size passed to
free()
-like functions!).However, no d'tor is called. If 'B' defines a destructor or contains any members with a non-trivial destructor they will not be called, causing a potential memory leak. This is not the case in your code sample, however.
Unless the base class destructor is virtual, it's undefined behaviour. See 5.3.5/4: