This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: problems with constructors
- To: Ross Smith <ross dot s at ihug dot co dot nz>, egcs-bugs at cygnus dot com
- Subject: Re: problems with constructors
- From: "C. van Reeuwijk" <C dot vanReeuwijk at twi dot tudelft dot nl>
- Date: Mon, 16 Nov 1998 10:54:57 +0100
- Organization: Delft University of Technology
- References: <006601be0f66$411ec910$a8a01dcb@animal.ihug.co.nz>
> >You are right, 'fullindex' is of a base class type.
> >
> >Strictly speaking, you are right; it is not necessarily true that the
> >pointer to the base class is the same as the pointer to the subclass.
> >However:
> >
> >(a) I should still be able to do 'delete fullindex;', but this statement
> >crashes. If I link in 'Electric fence', it tells me 'fullindex' is not a
> >malloc'ed pointer. The difference in pointers is a symptom of the problem;
> >the real problem is that 'delete fullindex;' crashes.
>
> Yes, you should be able to delete fullindex, *provided* the base class
> (the type fullindex is declared to point to) has a virtual destructor.
> (Does it? If not, I'd expect the delete to have nasty consequences.)
<Cough> Oops. You are absolutely right. The base class didn't have a
virtual destructor, and now that it has one the problem is gone. Sorry
everyone for wasting time.
BTW, this is a nasty gotcha. Is there a way to let the compiler warn me
about this?
> I'm not familiar with Electric Fence -- from your description of the
> symptoms, my first guess would be that EF only knows about C and
> doesn't understand polymorphism.
Electric fence is a replacement library for malloc and free that does
extensive validity checking. Very useful, although in this case perhaps
misleading.
> * * * * *
> "Screw up your courage. You've screwed up everything else."
Yeah, yeah, thank you. :-)
--
Kees van Reeuwijk, Delft University of Technology
http://www.pds.twi.tudelft.nl/~reeuwijk