Segfault with delete[] operator & virtually derived classes
JP Mercury
swirlee@stickist.com
Sat Mar 5 12:43:00 GMT 2005
Hi there,
I'm writing a memory manager and having some trouble. I have base class A and
derived class B. I am getting a segfault during the delete[] operator. This
simplified test program illustrates the problem:
// Base
class A {
public:
A() {};
virtual ~A() { printf("a"); };
virtual void func1() {};
int data1, data2;
};
// Derived
class B : public A {
public:
B() {};
virtual ~B() { printf("b "); };
int data3, data4;
};
main() {
A *list = new B[10]; // Allocate 10x derived, store first in A *list
delete[] list; // This segfaults
// delete[] ((B *) list); // This doesn't
};
Is it that the array delete operator isn't aware of polymorphism the way the
regular delete operator is?
I thought about allocating/deleting the list as char * of the appropriate
size, but I want the constructors and vtables to be setup properly.
Stuck.. Don't know how to solve this one.
Help?
Thanks,
-Mercury
More information about the Gcc-help
mailing list