Segfault with delete[] operator & virtually derived classes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux