Deleting memory allocated to the derived class

R

Rohit

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.
 
I

Ivan Vecerina

Rohit said:
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
 
S

Sumit Rajan

Rohit said:
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.
 
P

Pete Becker

Ivan said:
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.
 
I

Ivan Vecerina

Pete Becker said:
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
 
V

Victor Bazarov

Ivan said:
[...]
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
 
P

Prawit Chaivong

Rohit said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top