TITLE: Dynamic binding and deleting arrays of objects PROBLEM: gray@celia.UUCP (Steve Gray) I tried to post this once before but it didn't seem to take... so if this is a duplicate, my apologies: I have the following: struct A { A( void) {}; virtual ~A( void) {}; }; struct B : A { int *ptr; B() { ptr = (int *) malloc( 10); }; virtual ~B() { cfree( ptr); }; }; main() { A *aPtr; B *bPtr; bPtr = new B[10]; aPtr = (A *) bPtr; delete[10] aPtr; } This correctly calls the destructor for B. However, on the second element in the array ~B() crashes because the "this" pointer is wrong. It has only been advanced by sizeof(A) bytes (not sizeof(B) bytes). This is bad. Is it a bug? Am I doing something wrong? RESPONSE: p j l @cs.uiuc.edu (Paul Lucas), 1 Dec 92 AT&T Bell Laboratories *****> Cast is unnecessary. *****> So is the '10' . *****> No. *****> Yes; you're thinking that vectors are first-class objects in C++ -- they're not. The ++ operator is well-defined to increment by the size of object of the base type of the pointer, so: ++aPtr; // won't work won't work either. If ++ doesn't work, for a pointer that really points to an object of a derived type, how can you expect delete to work? If this worked, that would also imply heterogenous vectors (which is a contradiction since vectors are for storage of the same type).