views:

186

answers:

5

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?

p.s. I have tried g++ and vc++2008 and they won't leak. But I would like to know what is correct in C++ standard.

+8  A: 

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.

anon
+2  A: 

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.

Alexander Gessler
Where do you see a "free-like function" ? One of the reasons C++ uses a `delete` __operator__ instead of a function is a direct compiler implementation. The compiler _does_ know `sizeof(A)`, and it also knows that A has no virtual destructor, so it is allowed call `FourBytePool::free(void* p)` behind the scenes.
MSalters
+1  A: 

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.

sharptooth
+1  A: 

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.

psychotik
+5  A: 

It's technically undefined behaviour, but in practice it's likely to work, since there are no destructors involved.

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.

James Hopkin