Thread View: comp.std.c++
3 messages
3 total messages
Started by Douglas C. Schmidt
Fri, 04 Aug 1995 22:28
virtual delete for arrays
Author: Douglas C. Schmidt
Date: Fri, 04 Aug 1995 22:28
Date: Fri, 04 Aug 1995 22:28
72 lines
2169 bytes
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/
Re: virtual delete for arrays
Author: Steve Clamage
Date: Tue, 08 Aug 1995 16:53
Date: Tue, 08 Aug 1995 16:53
12 lines
904 bytes
904 bytes
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
Re: virtual delete for arrays
Author: Steve Clamage
Date: Mon, 14 Aug 1995 06:50
Date: Mon, 14 Aug 1995 06:50
13 lines
961 bytes
961 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads