 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
amit_edge@yahoo.com Guest
|
Posted: Fri Apr 29, 2005 6:48 am Post subject: Virtual Destructors? |
|
|
Can anyone tell me the reason for this behaviour of virtual
destructors?
#include<stdio.h>
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("nDestroying B"); }
};
void main()
{
b *b1=new b;
a *a1=b1;
a1->~a(); //calls Destructor B and A
printf("%dn",a1->i); // prints 10
delete b1; //calls Destructor A only!!!!
printf("%dn",a1->i); // prints junk
}
|
|
| Back to top |
|
 |
roberth+news@ifi.uio.no Guest
|
Posted: Fri Apr 29, 2005 7:09 am Post subject: Re: Virtual Destructors? |
|
|
[email]amit_edge (AT) yahoo (DOT) com[/email] wrote:
| Quote: | Can anyone tell me the reason for this behaviour of virtual
destructors?
|
Your program is ill-formed, and when run, (after changing void main
to int main, so that it will compile, of course) anything can
happen.
--
Robert Bauck Hamar
|
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Fri Apr 29, 2005 7:18 am Post subject: Re: Virtual Destructors? |
|
|
<amit_edge (AT) yahoo (DOT) com> wrote
| Quote: | Can anyone tell me the reason for this behaviour of virtual
destructors?
#include<stdio.h
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("nDestroying B"); }
};
|
You want to change this to "int main()". For more details, see:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
| Quote: | void main()
{
b *b1=new b;
a *a1=b1;
a1 and b1 point to the same object. |
| Quote: | a1->~a(); //calls Destructor B and A
now you destroy the object. |
| Quote: | printf("%dn",a1->i); // prints 10
Here, you invoke undefined behaviour (UB) by trying to play around with an |
object that does not exist.
| Quote: | delete b1; //calls Destructor A only!!!!
You delete an object twice! Further UB, but now it really doesn't matter |
anymore since we're already in the realm of undefined behaviour.
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.2
| Quote: | printf("%dn",a1->i); // prints junk
|
More of the same.
UB+anything = UB
Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com>
|
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Fri Apr 29, 2005 7:29 am Post subject: Re: Virtual Destructors? |
|
|
"Sumit Rajan" <sumit.rajan (AT) gmail (DOT) com> wrote
| Quote: |
[email]amit_edge (AT) yahoo (DOT) com[/email]> wrote in message
news:1114757285.175718.35040 (AT) f14g2000cwb (DOT) googlegroups.com...
Can anyone tell me the reason for this behaviour of virtual
destructors?
#include<stdio.h
class a
{
public:
int i;
a() { i=10; }
virtual ~a() { printf("nDestroying A"); }
};
class b: public a
{
public:
~b(){ printf("nDestroying B"); }
};
You want to change this to "int main()". For more details, see:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3
void main()
{
b *b1=new b;
a *a1=b1;
a1 and b1 point to the same object.
a1->~a(); //calls Destructor B and A
now you destroy the object.
|
Oops!
The destructor is called but the memory is not released.
Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com>
|
|
| Back to top |
|
 |
amit_edge@yahoo.com Guest
|
Posted: Fri Apr 29, 2005 8:40 am Post subject: Re: Virtual Destructors? |
|
|
That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??
|
|
| Back to top |
|
 |
Jean-Sebastien Samson Guest
|
Posted: Fri Apr 29, 2005 8:48 am Post subject: Re: Virtual Destructors? |
|
|
| Quote: | That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??
|
What Sumit told you is that you delete an object which no longer exists
(even if the memory was not released). This is evil and results in undefined
behaviour. Your program might also legitimately erase your hard drive and
launch a nuclear missile. The standard authorizes this. Therefore, it is
impossible to explain to you why you specifically get this result as any
result would qualify as undefined behaviour (even working perfectly during
all the development phase and blowing latter in the face of your most
important customer).
--
JS
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Fri Apr 29, 2005 9:12 am Post subject: Re: Virtual Destructors? |
|
|
<amit_edge (AT) yahoo (DOT) com> skrev i en meddelelse
news:1114764003.430276.247910 (AT) l41g2000cwc (DOT) googlegroups.com...
| Quote: | That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed. Why does "delete b1" then call the destructor of A only??
Destructor B destroyed successfully in the first attempt??
delete has a dual purpose: |
1) destroy the object by calling the objects destructor
2) releasing the memory.
You are only allowed to call a destructor once - calling it more times than
one causes undefined behaviour. Also, there is almost never any reason to
call the destructor explicitly as you did. What was the purpose of that?
/Peter
|
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Fri Apr 29, 2005 9:24 am Post subject: Re: Virtual Destructors? |
|
|
<amit_edge (AT) yahoo (DOT) com> wrote
| Quote: | That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
|
From the C++ Standard, 12.4/13:
"Once a destructor is invoked for an object, the object no longer exists;
....."
Maybe this will be helpful, too:
http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9
| Quote: | destroyed. Why does "delete b1" then call the destructor of A only??
|
As mentioned in Jean-Sebastien Samson's posting, you are now attempting to
destroy an object that does not exist.
Regards,
Sumit.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com>
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Fri Apr 29, 2005 10:39 am Post subject: Re: Virtual Destructors? |
|
|
[email]amit_edge (AT) yahoo (DOT) com[/email] wrote:
| Quote: | That is what I am asking Sumit. OK we make main() return int, now tell
me how is it that the destructor is called but the object is not
destroyed.
|
The object is destroyed. That's what the destructor does. But the memory
isn't released.
| Quote: | Why does "delete b1" then call the destructor of A only??
|
a1 and b1 are just pointers. By writing "a *a1=b1;", you just made a1 point
to the very same object that b1 points to. So there are _not_ two object,
but still only one. By "a1->~a()", you destroy that object, so now there is
no object left. You still have the memory that the object was in, but C++
doesn't allow you to do much with it. Especially not to try to destroy the
(now non-existing) object again. "delete b1;", when used correctly, does
that. It first destroys the object, then releases the memory it occupied.
| Quote: | Destructor B destroyed successfully in the first attempt??
|
At that point, there still was an object that could be destroyed.
|
|
| Back to top |
|
 |
|
|
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
|
|