This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Segfault with delete[] operator & virtually derived classes
- From: "JP Mercury" <swirlee at stickist dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 4 Mar 2005 20:24:56 -0700
- Subject: Segfault with delete[] operator & virtually derived classes
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