Topic: virtual delete for arrays


Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/08/14
Raw View
In article b7k@tango.cs.wustl.edu, schmidt@tango.cs.wustl.edu (Douglas C. Schmidt) writes:
>Hi,
>
>Can someone please clarify something for me?  It would appear that in
>SunC++ 4.0.1 and G++ 2.6.3 a base class pointer cannot be used to
>delete an array of objects of a derived class.

That's correct. To quote the draft standard on the subject of "delete[]",
"if the dynamic type of the object to be deleted differs from its static
type, the behavior is undefined."
---
Steve Clamage, stephen.clamage@eng.sun.com







Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/08/08
Raw View
In article b7k@tango.cs.wustl.edu, schmidt@tango.cs.wustl.edu (Douglas C. Schmidt) writes:
>Hi,
>
>Can someone please clarify something for me?  It would appear that in
>SunC++ 4.0.1 and G++ 2.6.3 a base class pointer cannot be used to
>delete an array of objects of a derived class.

That's correct. To quote the draft standard on the subject of "delete[]",
"if the dynamic type of the object to be deleted differs from its static
type, the behavior is undefined."
---
Steve Clamage, stephen.clamage@eng.sun.com







Author: schmidt@tango.cs.wustl.edu (Douglas C. Schmidt)
Date: 1995/08/04
Raw View
Hi,

Can someone please clarify something for me?  It would appear that in
SunC++ 4.0.1 and G++ 2.6.3 a base class pointer cannot be used to
delete an array of objects of a derived class.  For instance, with
SunC++ the program below gives an error saying that the delete does
not correspond to any new (g++ dumps core).

Thus, it appears that delete [] (_vec_delete) in these implementations
take a pointer to the destructor based on the type of the pointer,
i.e., no facilty is made to support a virtual destructor.

My questions are:

 1. is this how delete[] is defined to work in the draft standard?
 2. how does one work around this in practice? i.e., what are
    the idioms for deleting a polymorphic vector pointer properly?

Thanks,

 Doug

----------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class base
{
protected:
  int a;
public:
  base()
  {
    printf("In base constructor this = 0x%lx\n", (unsigned long) this);
  };
  virtual ~base()
  {
    printf("destructor of base called this = 0x%lx\n", (unsigned long)
    this);
  };
};

class derive : public base
{
public:
  derive()
  {
    printf("In derive constructor this = 0x%lx\n", (unsigned long) this);
  };
  virtual ~derive()
  {
    printf("destructor of derived this = 0x%lx\n", (unsigned long) this);
  };
private:
  int b; // If I comment this out (which makes base and derive same size?)
};

int
main (void)
{
  base *y = new derive[2];
  printf("deleting y = 0x%lx\n", (unsigned long) y);
  delete [] y;
  return 0;
}

--
Dr. Douglas C. Schmidt    (schmidt@cs.wustl.edu)
Department of Computer Science, Washington University
St. Louis, MO 63130. Work #: (314) 935-7538; FAX #: (314) 935-7302
http://www.cs.wustl.edu/~schmidt/