Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Parent class still callable after deletion

Reply
Thread Tools

Parent class still callable after deletion

 
 
Olumide
Guest
Posts: n/a
 
      02-09-2011
Hi,

I've been trying to figure out why the following bit of code does not
crash and still outputs the "Hello son" message althought its been
explicitly deleted. (BTW, I'm using the gcc 4.3.4 compiler.)

Thanks,

- Olumide

//////////////////////////////////////////////////////////////////////////////////////

#include <iostream>

class Parent
{
public:
void speak()
{
std::cout << "Hello son" << std::endl;
}
};

class Child
{
public:
Child(Parent* _p ) : m_parent( _p )
{

}

void speakToParent()
{
std::cout << m_parent << ":: " << this << std::endl;
m_parent->speak();
}

private:
Parent* m_parent;

};

int main()
{
Parent *p = new Parent();
Child *c = new Child( p );
delete p;
c->speakToParent();
return 0;
}
 
Reply With Quote
 
 
 
 
André Schreiter
Guest
Posts: n/a
 
      02-09-2011
Am 09.02.2011 15:30, schrieb Olumide:
> I've been trying to figure out why the following bit of code does not
> crash...


Undefined behavior.
 
Reply With Quote
 
 
 
 
gwowen
Guest
Posts: n/a
 
      02-09-2011
On Feb 9, 2:30*pm, Olumide <50...@web.de> wrote:
> Hi,
>
> I've been trying to figure out why the following bit of code does not
> crash and still outputs the "Hello son" message althought its been
> explicitly deleted. (BTW, I'm using the gcc 4.3.4 compiler.)


Calling methods on deleted objects is "undefined behaviour". This
means that anything is allowed, and nothing is guaranteed -- crashing
is certainly one possiblity; behaving exactly as if you hadn't deleted
the object is another possibility, in theory any other outcome is also
possible (Usenet tradition demands that I add "including demons flying
out of your nose" at this point). Whatever your program does (or
seems to do), it cannot be relied upon. Next time you run the
program, something else might happen.
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      02-09-2011
On Feb 9, Olumide wrote:
> > Hi,

>
> > I've been trying to figure out why the following bit of code does not
> > crash and still outputs the "Hello son" message althought its been
> > explicitly deleted. (BTW, I'm using the gcc 4.3.4 compiler.)



On 9 Feb., gwowen wrote:
> Calling methods on deleted objects is "undefined behaviour". *This
> means that anything is allowed, and nothing is guaranteed -- crashing
> is certainly one possiblity; behaving exactly as if you hadn't deleted
> the object is another possibility, in theory any other outcome is also
> possible (Usenet tradition demands that I add "including demons flying
> out of your nose" at this point). *Whatever your program does (or
> seems to do), it cannot be relied upon. *Next time you run the
> program, something else might happen.


Note that we exaggerate vastly in order to make it quite clear that
such errors should be avoided with paramount interest. Of course,
nothing as bad is going to happen if you stick to your particular
platform. However, should you ever consider moving to another platform
(or maybe just the next version of your compiler), you may be in for a
surprise.

Regards,
Stuart
 
Reply With Quote
 
itaj sherman
Guest
Posts: n/a
 
      02-10-2011
On Feb 9, 4:30*pm, Olumide <50...@web.de> wrote:
> Hi,
>
> I've been trying to figure out why the following bit of code does not
> crash and still outputs the "Hello son" message althought its been
> explicitly deleted. (BTW, I'm using the gcc 4.3.4 compiler.)
>
> Thanks,
>
> - Olumide
>
> //////////////////////////////////////////////////////////////////////////////////////
>
> * * #include <iostream>
>
> * * class Parent
> * * {
> * * public:
> * * * * void speak()
> * * * * {
> * * * * * * * * std::cout << "Hello son" << std::endl;
> * * * * }
> * * };
>
> * * class Child
> * * {
> * * public:
> * * * * Child(Parent* _p ) : m_parent( _p )
> * * * * {
>
> * * * * }
>
> * * * * void speakToParent()
> * * * * {
> * * * * * * * * std::cout << m_parent << ":: " << this << std::endl;
> * * * * * * * * m_parent->speak();
> * * * * }
>
> * * private:
> * * * * Parent* m_parent;
>
> * * };
>
> * * int main()
> * * {
> * * * * Parent *p = new Parent();
> * * * * Child *c = new Child( p );
> * * * * delete p;
> * * * * c->speakToParent();
> * * * * return 0;
> * * }


As they said, it is undefined behaviour.
Nevertheless, some compilers (i dont know gcc) have nice debugging
features, so that when you compile for debug version and execute, the
debugger will break and tell you when you reach undefined behaviour in
some situations when possible (certainly this one is possible).
I would expect release compilation (optimized) not to do such things,
as trying to check for such runtime errors will slow execution.

itaj
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS TreeView - child checkbox items still checked after parent hasbeen unchecked brendan_gallagher_2001@yahoo.co.uk ASP .Net 1 01-10-2011 05:46 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
Making Non Callable Objects Callable exiquio Python 2 10-07-2008 06:02 PM
Obtaining a callable class method object from a specific class Nathan Duran Python 1 04-10-2008 08:08 PM
verify whether "str" is still callable in Python 2.5.1 Ge Chunyuan Python 3 08-13-2007 09:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57