C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Deleting memory allocated to the derived class

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Rohit
Guest





PostPosted: Tue Feb 15, 2005 1:08 pm    Post subject: Deleting memory allocated to the derived class Reply with quote



Iam writing an application that uses an abstract base class and a
derived class that is implementation of the abstract base class. Say I
have this piece of code:

Derived *ptrToDerived=NULL;
Base *ptrToBase=NULL;
ptrToDerived= new Derived;
ptrToBase=ptrToDerived;
.....................
.....................
//Do stuff..........
.....................
delete ptrToBase;

Now my doubt is: will the memory be deleted properly. Or what will be
the behaviour of delete in this case, because delete first calls the
destructor of the class, and I have no destructor in the base class.

Back to top
Ivan Vecerina
Guest





PostPosted: Tue Feb 15, 2005 1:23 pm    Post subject: Re: Deleting memory allocated to the derived class Reply with quote



"Rohit" <rohit_ganda (AT) yahoo (DOT) com> wrote

Quote:
Iam writing an application that uses an abstract base class and a
derived class that is implementation of the abstract base class. Say I
have this piece of code:

Derived *ptrToDerived=NULL;
Base *ptrToBase=NULL;
ptrToDerived= new Derived;
ptrToBase=ptrToDerived;
....................
....................
//Do stuff..........
....................
delete ptrToBase;

Now my doubt is: will the memory be deleted properly. Or what will be
the behaviour of delete in this case, because delete first calls the
destructor of the class, and I have no destructor in the base class.

For delete to work correctly (i.e. not cause Undefined Behavior),
class Base must have a virtual destructor.
Either:
virtual ~Base() = 0; // abstract virtual, not implemented
Or:
virtual ~Base() {} // implemented inline or not, as you prefer
With that, you'll have the guarantee that the memory is properly
released, and that the appropriate (subclass) destructor is called.

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Back to top
Sumit Rajan
Guest





PostPosted: Tue Feb 15, 2005 1:24 pm    Post subject: Re: Deleting memory allocated to the derived class Reply with quote




"Rohit" <rohit_ganda (AT) yahoo (DOT) com> wrote

Quote:
Iam writing an application that uses an abstract base class and a
derived class that is implementation of the abstract base class. Say I
have this piece of code:

Derived *ptrToDerived=NULL;
Base *ptrToBase=NULL;
ptrToDerived= new Derived;
ptrToBase=ptrToDerived;
....................
....................
//Do stuff..........
....................
delete ptrToBase;

Now my doubt is: will the memory be deleted properly. Or what will be
the behaviour of delete in this case, because delete first calls the
destructor of the class, and I have no destructor in the base class.


http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.5

Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com>



Back to top
Pete Becker
Guest





PostPosted: Tue Feb 15, 2005 1:40 pm    Post subject: Re: Deleting memory allocated to the derived class Reply with quote

Ivan Vecerina wrote:
Quote:
For delete to work correctly (i.e. not cause Undefined Behavior),
class Base must have a virtual destructor.
Either:
virtual ~Base() = 0; // abstract virtual, not implemented

The destructor has to be implemented. It's called from the derived
class's destructor.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Back to top
Ivan Vecerina
Guest





PostPosted: Tue Feb 15, 2005 2:07 pm    Post subject: Re: Deleting memory allocated to the derived class Reply with quote

"Pete Becker" <petebecker (AT) acm (DOT) org> wrote

Quote:
Ivan Vecerina wrote:
For delete to work correctly (i.e. not cause Undefined Behavior),
class Base must have a virtual destructor.
Either:
virtual ~Base() = 0; // abstract virtual, not implemented

The destructor has to be implemented. It's called from the derived class's
destructor.

Woops, that's right.

The weird thing (part of what confused my memories) is that an *abstract*
virtual desctructor cannot be defined inline within the class body:
virtual ~Base() = 0 {} //illegal syntax


Thanks,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Back to top
Victor Bazarov
Guest





PostPosted: Tue Feb 15, 2005 2:53 pm    Post subject: Re: Deleting memory allocated to the derived class Reply with quote

Ivan Vecerina wrote:
Quote:
[...]
The weird thing (part of what confused my memories) is that an *abstract*

The term is *pure*. "Abstract" is the term describing the class.

Quote:
virtual desctructor cannot be defined inline within the class body:
virtual ~Base() = 0 {} //illegal syntax

No function can. A function declared pure can an implementation, but only
defined outside the class definition.

V

Back to top
Rohit
Guest





PostPosted: Thu Feb 24, 2005 4:57 am    Post subject: Re: Deleting memory allocated to the derived class Reply with quote

Thanks All

Back to top
Prawit Chaivong
Guest





PostPosted: Tue Mar 01, 2005 10:34 am    Post subject: Re: Deleting memory allocated to the derived class Reply with quote

"Rohit" <rohit_ganda (AT) yahoo (DOT) com> wrote

Quote:
Iam writing an application that uses an abstract base class and a
derived class that is implementation of the abstract base class. Say I
have this piece of code:

Derived *ptrToDerived=NULL;
Base *ptrToBase=NULL;
ptrToDerived= new Derived;
ptrToBase=ptrToDerived;
....................
....................
//Do stuff..........
....................
delete ptrToBase;

Now my doubt is: will the memory be deleted properly. Or what will be
the behaviour of delete in this case, because delete first calls the
destructor of the class, and I have no destructor in the base class.

delete is find. BUT delete[] can cause undefind behaviour.

Derived *ptrToDerived=NULL;
Base *ptrToBase=NULL;
ptrToDerived = new Derived[20];
ptrToBase=ptrToDerived;
......
.....
.....
delete[] ptrToBase;

regards,

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.