Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: delete [] o...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 5 of 13 Topic 45621 of 48022
Post > Topic >>

Re: delete [] of Derived objects through Base object which has

by James Kanze <james.kanze@[EMAIL PROTECTED] > Apr 29, 2008 at 02:38 AM

On Apr 29, 4:32 am, "subramanian10...@[EMAIL PROTECTED]
 India"
<subramanian10...@[EMAIL PROTECTED]
> wrote:
> Suppose

> class Base
> {
> public:
> virtual ~Test() { ... }
> // ...
> };

> class Derived : public Base
> {
> public:
> virtual ~Derived() { ... }
> // ...
> };

> int main()
> {
>       Base* base_ptr  =3D new Derived[10]();
>       delete [] base_ptr;
>       return EXIT_SUCCESS;
> }

> If the Base class dtor is not not virtual, 'delete []
> base_ptr' has undefined behaviour.

> Will 'delete [] base_ptr' call each Derived class dtor because
> the Base::~Base() is virtual ? Is the deletion in the above
> code valid ?.  Or does this also invoke undefined behaviour ?

As Kai-Uwe has pointed out, it is undefined behavior.  More
generally, although the compiler will tranquilly convert
Derived* to Base* even if Derived* points to an array (because
it cannot know this), the resulting pointer can only be used as
a pointer to the first individual object.  In you case, for
example, no only is the delete[] undefined behavior, but any use
of base_ptr to access the allocated array (e.g. base_ptr[1])
would be as well.

In general, don't use array new; prefer std::vector.  And don't
try to make array elements polymorphic; it doesn't work.  (Array
elements are values, and polymorphism only works through
pointers or references.  If you need an array of polymorphic
types, you must use std::vector< Base* >, allocating and
deallocating each one manually.)


--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 13 Posts in Topic:
delete [] of Derived objects through Base object which has virt
"subramanian100in@[E  2008-04-28 19:32:38 
Re: delete [] of Derived objects through Base object which has
Sam <sam@[EMAIL PROTEC  2008-04-28 22:49:23 
Re: delete [] of Derived objects through Base object which has
"Jim Langston"   2008-04-28 22:14:10 
Re: delete [] of Derived objects through Base object which has
Kai-Uwe Bux <jkherciue  2008-04-29 02:30:39 
Re: delete [] of Derived objects through Base object which has
James Kanze <james.kan  2008-04-29 02:38:59 
Re: delete [] of Derived objects through Base object which has v
"Jim Langston"   2008-04-29 02:48:22 
Re: delete [] of Derived objects through Base object which has v
"Jim Langston"   2008-04-29 03:05:02 
Re: delete [] of Derived objects through Base object which has
"subramanian100in@[E  2008-04-29 04:52:10 
Re: delete [] of Derived objects through Base object which has
Barry <dhb2000@[EMAIL   2008-04-29 19:58:50 
Re: delete [] of Derived objects through Base object which has
xavier <xavier>   2008-05-13 00:04:19 
Re: delete [] of Derived objects through Base object which has
Ian Collins <ian-news@  2008-05-13 10:06:11 
Re: delete [] of Derived objects through Base object which has
James Kanze <james.kan  2008-04-29 05:45:51 
Re: delete [] of Derived objects through Base object which has
James Kanze <james.kan  2008-05-13 01:53:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 11:18:38 CDT 2008.