Parent class still callable after deletion

Discussion in 'C++' started by Olumide, Feb 9, 2011.

  1. Olumide

    Olumide Guest

    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;
    }
     
    Olumide, Feb 9, 2011
    #1
    1. Advertising

  2. 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.
     
    André Schreiter, Feb 9, 2011
    #2
    1. Advertising

  3. Olumide

    gwowen Guest

    On Feb 9, 2:30 pm, 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.)


    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.
     
    gwowen, Feb 9, 2011
    #3
  4. 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
     
    Stuart Redmann, Feb 9, 2011
    #4
  5. Olumide

    itaj sherman Guest

    On Feb 9, 4:30 pm, 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.)
    >
    > 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
     
    itaj sherman, Feb 10, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ge Chunyuan
    Replies:
    3
    Views:
    317
    Lawrence Oluyede
    Aug 13, 2007
  2. Nathan Duran
    Replies:
    1
    Views:
    340
    Arnaud Delobelle
    Apr 10, 2008
  3. exiquio
    Replies:
    2
    Views:
    546
    exiquio
    Oct 7, 2008
  4. Replies:
    1
    Views:
    2,327
  5. Ulrich Eckhardt
    Replies:
    6
    Views:
    114
    Peter Otten
    Jul 12, 2013
Loading...

Share This Page