🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1660

virtual delete for arrays

#1660
From: Douglas C. Schmidt
Date: Fri, 04 Aug 1995 22:28
72 lines
2169 bytes
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/
Technical Details
Message-ID: <3vu6tv$b7k@tango.cs.wustl.edu>
Path: arc1-ca.pugleaf.net!tango.cs.wustl.edu!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!tango.cs.wustl.edu!not-for-mail
From: schmidt@tango.cs.wustl.edu (Douglas C. Schmidt)
Newsgroups: comp.std.c++
Subject: virtual delete for arrays
Date: 4 Aug 1995 17:28:15 -0500
Organization: Computer Science Department, Washington University.
Message-ID: <3vu6tv$b7k@tango.cs.wustl.edu>
NNTP-Posting-Host: tango.cs.wustl.edu