This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: operator delete [] and virtual destructors


> But, for the B *vector*, the compiler calls A::operator delete [].
> Shouldn't it call B::operator delete [], just like it calls
> B::operator delete for single B-objects?

Thanks for your bug report. This is not a bug in the compiler, but in
your code. ISO C++, 5.3.5, [expr.delete]/3, says

# In the second alternative (delete array) if the dynamic type of the
# object to be deleted differs from its static type, the behavior is
# undefined.

Since you are deleting a B array through an A pointer, the behaviour
is undefined. The compiler can do anything it wants with your code.

If you question this line of reasoning, please discuss it in one of
the public C++ fora first, eg. comp.lang.c++.moderated, or
comp.std.c++.

From a technical point of view, this restriction is there because the
compiler cannot know the dynamic size of the elements of the array,
which would be necessary to iterate over the array and call each
element. Therefore, array deletes are guaranteed not to use a virtual
destructor, and g++ choses to always invoke the dtor non-virtually. If
B had any additional members, your code would crash.

Regards,
Martin

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]