delete(this)

Discussion in 'C++' started by Rahul, Feb 3, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I was wondering what the standard says about the following,

    class test
    {
    public: test()
    {
    delete(this);
    }
    ~test()
    {
    delete(this);
    }
    };

    int main()
    {
    test object;
    }

    Thanks in advance!!!
     
    Rahul, Feb 3, 2008
    #1
    1. Advertising

  2. Rahul

    Rolf Magnus Guest

    Rahul wrote:

    > Hi Everyone,
    >
    > I was wondering what the standard says about the following,
    >
    > class test
    > {
    > public: test()
    > {
    > delete(this);
    > }
    > ~test()
    > {
    > delete(this);
    > }
    > };
    >
    > int main()
    > {
    > test object;
    > }


    It says that this code has undefined behavior. You must not delete objects
    that haven't been created using new.
     
    Rolf Magnus, Feb 3, 2008
    #2
    1. Advertising

  3. Rahul

    peter koch Guest

    On 3 Feb., 14:27, Rahul <> wrote:
    > Hi Everyone,
    >
    >  I was wondering what the standard says about the following,
    >
    > class test
    > {
    >  public: test()
    >             {
    >                delete(this);
    >             }
    >            ~test()
    >             {
    >               delete(this);
    >            }
    >
    > };
    >
    > int main()
    > {
    >  test object;
    >
    > }
    >
    >  Thanks in advance!!!


    This is full of undefined behavior, the first one being destruction of
    an object that had not yet been fully constructed, the second being
    calling delete on something not allocated by new, and the third being
    calling the destructor on the same object twice. There might be more,
    but technically the first one is all you can talk about,

    /Peter
     
    peter koch, Feb 3, 2008
    #3
  4. Rahul

    James Kanze Guest

    On Feb 3, 2:33 pm, Rolf Magnus <> wrote:
    > Rahul wrote:


    > > I was wondering what the standard says about the following,


    > > class test
    > > {
    > > public: test()
    > > {
    > > delete(this);
    > > }
    > > ~test()
    > > {
    > > delete(this);
    > > }
    > > };


    > > int main()
    > > {
    > > test object;
    > > }


    > It says that this code has undefined behavior. You must not
    > delete objects that haven't been created using new.


    Amongst other things. Think of what will happen in a new
    expresssion: "new test". I don't think deleting the return
    value of new is really a good idea either. And of course, if
    you call delete on a pointer to test, you'll end up double
    deleting.

    Delete this is pretty much a standard idiom, but not from the
    constructor or the destructor.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 3, 2008
    #4
  5. peter koch wrote:
    >> ~test()
    >> {
    >> delete(this);
    >> }


    > and the third being
    > calling the destructor on the same object twice.


    I wonder if that could cause an infinite loop with some compilers.
     
    Juha Nieminen, Feb 4, 2008
    #5
  6. Rahul

    James Kanze Guest

    On Feb 4, 2:48 pm, Juha Nieminen <> wrote:
    > peter koch wrote:
    > >> ~test()
    > >> {
    > >> delete(this);
    > >> }

    > > and the third being
    > > calling the destructor on the same object twice.


    > I wonder if that could cause an infinite loop with some compilers.


    An infinite loop, only if the compiler does tail recursion
    optimization, but an infinite recursion does seem likely, since
    the first thing a delete expression does is call the destructor.

    Well spotted.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 5, 2008
    #6
    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. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    640
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    402
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    320
    Jerry Coffin
    Dec 19, 2003
  4. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,852
    Mathieu Malaterre
    Aug 17, 2004
  5. Jef Driesen
    Replies:
    1
    Views:
    516
    Gernot Frisch
    Jan 19, 2005
Loading...

Share This Page