Parent class still callable after deletion

O

Olumide

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;
}
 
G

gwowen

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

Stuart Redmann

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
 
I

itaj sherman

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top