473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Jul 23 '05 #1
7 1946
"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

"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
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
"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
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
Thanks All

Jul 23 '05 #7
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Min | last post by:
CFool { public: void StartWorldWarIII() { /*** Launch Nuclear Missiles **/ } }; int main() { CFool* pFool = new CFool(); delete pFool;
6
by: Abhijeet | last post by:
I was just toying around idea of deleting this from a member function. Was expecting that any acess to member variable or function after deleting sould give me dump(segmetation violation).Cause now...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
2
by: Manisha | last post by:
Hi, I am creating a C++ dll which is used to process data passed to it through one of its exported functions. It should be able to process 160 simultaneous requests. For this reason, I have...
3
by: schizoid_man | last post by:
Hi, I have the following code snippets and I get a std::bad_alloc error where I think there should be none. I've attached the relevant bits of the base class, derived class and the .cpp file...
8
by: NAdir | last post by:
Hi, thank you for your help. My VB.Net application contains a document that the user can refresh at any time. The refresh works fine and needs to loop through few datatables (hundreds of rows)....
18
by: happyvalley | last post by:
Hi, basically, the test function get a char pointer, and assigned a string to it. then the string is passed back by the call-by-reference mechanism. in test(), I reallocate some memory for the...
1
by: Pat | last post by:
Hi all, I have a really awkward situation that is causing memory leak problems. I'm passing data to a driver, and unfortunately, the driver code is not something I can change, and it is written...
12
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.