By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Deleting memory allocated to the derived class

P: n/a
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.

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Rohit" <ro*********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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
Jul 23 '05 #2

P: n/a

"Rohit" <ro*********@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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-lit....html#faq-20.5

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #3

P: n/a
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4

P: n/a
"Pete Becker" <pe********@acm.org> wrote in message
news:6s********************@rcn.net...
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
Jul 23 '05 #5

P: n/a
Ivan Vecerina wrote:
[...]
The weird thing (part of what confused my memories) is that an *abstract*
The term is *pure*. "Abstract" is the term describing the class.
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
Jul 23 '05 #6

P: n/a
Thanks All

Jul 23 '05 #7

P: n/a
"Rohit" <ro*********@yahoo.com> wrote in message news:<11*********************@z14g2000cwz.googlegr oups.com>...
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,
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.