Topic: use of virtual delete operator on arrays?


Author: Scott Baillie <scottxb@mpx.com.au>
Date: 2000/03/13
Raw View
Hi,

In my code I have used the virtual delete operator on
an array and I got a seg fault at runtime.

I tried using 3 different compilers and got 3
different results.

VisC++6      : My code worked.
InpriseBcc55 : My code failed gracefully.
GNU C++      : I got a seg fault.

I posted a bug report to the GNU C++ compiler maintainers
and they told me that it was a bug in my code rather
than a bug in the compiler. I am not sure.

Can anyone tell me if the C++ standard allows
the virtual delete operator on array's?

Here is an example :

class Base {
public:
  virtual ~Base() { printf("~Base\n") ; }
} ;

class Derived : public Base {
public:
  virtual ~Derived() { printf("~Derived\n") ; }
} ;

int main(int argc,char* argv[])
{
  Base* pBase=new Derived[2] ;
  delete [] pBase ;
  return(0) ;
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Dietmar Kuehl <dietmar.kuehl@claas-solutions.de>
Date: 2000/03/13
Raw View
Hi,
In article <38CB8A7B.E7DE97AB@mpx.com.au>,
  Scott Baillie <scottxb@mpx.com.au> wrote:
> I posted a bug report to the GNU C++ compiler maintainers
> and they told me that it was a bug in my code rather
> than a bug in the compiler. I am not sure.

I am sure: Your code is broken! It is illegal to delete an array object
with a different static type than the type when it was allocated. The
details are described in section 5.3.5, expr.delete, paragraph 3:

  ... In the second alternative (delete array) if the dynamic type of
  the object to be deleted differs from its static type, the behavior
  is undefined.

> Can anyone tell me if the C++ standard allows
> the virtual delete operator on array's?

Of course, the objects in an array may have a virtual destructor.
However, this is irrelevant because it is never used during release of
an array: The type of the objects is known at compile time and there is
no need to have a virtual destructor.

> int main(int argc,char* argv[])
> {
>   Base* pBase=new Derived[2] ;
>   delete [] pBase ;

This is undefined bahavior: The dynamic type of the object pointed to
by 'pBase' is 'Derived*' but the static type is 'Base*'. Whether the
destructor of 'Base' is virtual or not is irrelevant to this example:
It is undefined behavior anyway.
--
<mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Richard Parkin" <rparkin@msi-eu.com>
Date: 2000/03/14
Raw View
Scott Baillie <scottxb@mpx.com.au> wrote in message
news:38CB8A7B.E7DE97AB@mpx.com.au...

> Can anyone tell me if the C++ standard allows
> the virtual delete operator on array's?
>
> Here is an example :
>
> class Base {
> public:
>   virtual ~Base() { printf("~Base\n") ; }
> } ;
>
> class Derived : public Base {
> public:
>   virtual ~Derived() { printf("~Derived\n") ; }
> } ;
>
> int main(int argc,char* argv[])
> {
>   Base* pBase=new Derived[2] ;

You have created an array of two Derived.

You have a pointer to the first one.

How is the compiler going to work out where the second one is given only the
pointer to the first (so we can call it's destructor)? It does this by using
the type of the pointer it is deleted by

(pBase +1) ? No, if sizeof(Derived) is not sizeof(Base) that'll point to the
wrong place.

Answer : ((Derived*)pBase +1)

>   delete [] pBase ;

Here you *must* do
delete[] (Derived*)pBase;

>   return(0) ;
> }

This is no so much a problem with virtual, or destructors, but of working
out *where* an object is - it requires a pointer to the start of the array,
and the size of the objects in the array.

Ric







---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Paul Black <paul.black@oxsemi.com>
Date: 2000/03/14
Raw View
Scott Baillie <scottxb@mpx.com.au> wrote:
> I posted a bug report to the GNU C++ compiler maintainers
> and they told me that it was a bug in my code rather
> than a bug in the compiler. I am not sure.
>
> Can anyone tell me if the C++ standard allows
> the virtual delete operator on array's?

You have to delete an array through the same type of pointer that was
returned from new[]. In your case, the argument to delete[] has to be a
"Derived *" as the array is an array of Deriveds. The error is in your
code, not the compiler.

Paul

P.S. Dubious time on the post, is a moderator's machine out by a few
hours/timezones?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Bill Wade" <bill.wade@stoner.com>
Date: 2000/03/16
Raw View
>P.S. Dubious time on the post, is a moderator's machine out by a few
>hours/timezones?

It seems that one of the moderator's "approvals" always carry a time labeled
CST (which I believe, in his case is central Australian time, GMT +10).
Some readers (including the one I use) interpret CST as central US, GMT -6.

IIRC some other moderator's "approvals" carry a time labeled GMT.  A quick,
unscientific survey seems to show that our friend down under is doing a
significant fraction of the approvals right now, so I wouldn't recommend
giving him a hard time.  OTOH if you can show him how to improve the
situation, I'd appreciate it.



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]