| View previous topic :: View next topic |
| Author |
Message |
Rohit Guest
|
Posted: Tue Feb 15, 2005 1:08 pm Post subject: Deleting memory allocated to the derived class |
|
|
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
|
Posted: Tue Feb 15, 2005 1:23 pm Post subject: Re: Deleting memory allocated to the derived class |
|
|
"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
|
Posted: Tue Feb 15, 2005 1:24 pm Post subject: Re: Deleting memory allocated to the derived class |
|
|
"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
|
Posted: Tue Feb 15, 2005 1:40 pm Post subject: Re: Deleting memory allocated to the derived class |
|
|
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
|
Posted: Tue Feb 15, 2005 2:07 pm Post subject: Re: Deleting memory allocated to the derived class |
|
|
"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
|
Posted: Tue Feb 15, 2005 2:53 pm Post subject: Re: Deleting memory allocated to the derived class |
|
|
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
|
Posted: Thu Feb 24, 2005 4:57 am Post subject: Re: Deleting memory allocated to the derived class |
|
|
Thanks All
|
|
| Back to top |
|
 |
Prawit Chaivong Guest
|
Posted: Tue Mar 01, 2005 10:34 am Post subject: Re: Deleting memory allocated to the derived class |
|
|
"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 |
|
 |
|