I get segmentation fault exception on the next code:
class A {
public:
A() {}
virtual ~A(){}
double m_d;
};
class B : public A {
public:
B() {}
virtual ~B(){}
int x;
};
int main()
{
A* ptr = new B[5];
delete[] ptr;
return 0;
}
If delete d’tors, no exception.
Expected to NOT receive an exception.
Compiler: g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
2
Answers
Array
delete
doesn’t respect virtual destructors. Attempting to pass a base class pointer to it causes undefined behavior.[expr.delete]/2
Note the second list excluding "base class subobject…" part.
[expr.delete]/3
Where "static type" is the type as determined at compile-time, "dynamic type" is the type of the most-derived object determined at runtime, and "similar" more or less means "the same type", but allows some differences in constness and more.
This is not allowed, as described here: Why is it undefined behavior to delete[] an array of derived objects via a base pointer?
Excerpt: C++03 Standard under
5.3.5 [expr.delete] p3
: In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefinedI also made, out of curiosity, logging version:
https://godbolt.org/z/Y7xGdKd3f
Clang: calls base class destructor using incorrect data
2.07634e-317 ; 0.5 ; 9.88131e-324 ; 2.07634e-317 ; 0.5 ; Hi, nothing crashed here!
Gnu: politely crashes
Program returned: 139
M$: works as expected 🙂
2 : 0.5 ; 2 : 0.5 ; 2 : 0.5 ; 2 : 0.5 ; 2 : 0.5 ; Hi, nothing crashed here