Segmentation error after deleting [] in the base class pointer

I have a piece of code here from which I do not understand why it leads to a segmentation error on line 22 (delete [] instruction). Could you explain this to me?

#include<iostream> #include<memory> class A { size_t a[1000]; public: virtual ~A() { } }; class B : public A { public: float b; virtual ~B() { } }; int main(int argc, char** argv){ A *b; b = new B[10]; delete[] b; return 0; } 

It is strange if class B does not have any member variables (that is, I will comment on the string "float b;"), then the code just works fine.

What is my mistake here?

+8
c ++ segmentation-fault delete-operator
source share
3 answers

Simply put, you have undefined behavior. You do not provide delete[] pointer you received from new[] . You might think that this is so, but for pointers in the version of the array they are the same, their static type must match. You have converted a pointer to a pointer to a base class.

In practice, when you don't have this added float , your implementation probably supports sizeof(B) == sizeof(A) . Thus, calls to the destructor and release functions do nothing harmful. But this is the same as undefined.

+11
source share

delete[] b; will try to delete an array of objects A, not an array of objects B. If class B does not have any member variables, then the sizes of these arrays are the same, which probably allows you to evade the bullet.

When delete is called, it will call the vtable of each element stored in the array to invoke the virtual destructor. It is assumed that the size of each element is sizeof(A) , therefore, if sizeof(B) is different, then access to the vtable of the second element will be performed with the wrong offset.

+3
source share

If you need polymorphism with an array, you need to create an array of pointers to the base class .

+1
source share

All Articles