Segfault with delete[] operator & virtually derived classes

lrtaylor@micron.com lrtaylor@micron.com
Mon Mar 7 18:06:00 GMT 2005


> you asked for an array of Bs, if you're not treating it as an array of
> Bs it's not gonna work.

Why not?  If B is derived from A, there should be nothing wrong with
storing pointers to B in an array of A pointers.  Although, it seems
that A and B would need to have virtual destructors.  I didn't read the
earlier part of the thread, so I don't know if that is the case here...

> ...you're lying to the compiler, and then kludging around the issue.

So, if B is derived from A and I do

A* a = new B;

am I lying to the compiler?  That's what inheritance and polymorphism is
all about!

Thanks,
Lyle


-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Nathan Sidwell
Sent: Monday, March 07, 2005 10:44 AM
To: JP Mercury
Cc: gcc-help@gcc.gnu.org
Subject: Re: Segfault with delete[] operator & virtually derived classes

JP Mercury wrote:
> On Mon, 07 Mar 2005 13:44:07 +0000, Nathan Sidwell wrote
> 
>>JP Mercury wrote:
>>
>>
>>>main() {
>>>  A *list = new B[10];      // Allocate 10x derived, store first in A
*list

> Someone else made this comment too. But I am not treating 'list' like
an array
> of B's. For example, when I access the array I do:

you asked for an array of Bs, if you're not treating it as an array of
Bs it's not gonna work.

> Can you see other issues I might have overlooked?

Yes, you're lying to the compiler, and then kludging around the issue.

nathan

-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery
LLC
nathan@codesourcery.com    ::
http://www.planetfall.pwp.blueyonder.co.uk





More information about the Gcc-help mailing list