Do I need to check for NULL before delete p?

Discussion in 'C++' started by sam, Jul 22, 2003.

  1. sam

    sam Guest

    For Example, If I delete two times like the below code, the program will
    behave differently. is this correct?
    As C++ FAQ the delete checks where or not f1 has memory. How can the
    following program can go wrong?

    class foo{
    //
    };
    int main{
    foo *f1 = new foo()
    delete f1;
    delete f1;
    return (0);
    }


    C++ FAQ
    [16.7] Do I need to check for NULL before delete p?

    No!

    The C++ language guarantees that delete p will do nothing if p is equal to
    NULL. Since you might get the test backwards, and since most testing
    methodologies force you to explicitly test every branch point, you should
    not put in the redundant if test.

    Wrong:

    if (p != NULL)
    delete p;

    Right:

    delete p;


    Thanks
    Sam
    sam, Jul 22, 2003
    #1
    1. Advertising

  2. sam

    Ron Natalie Guest

    "sam" <> wrote in message news:eek:lcTa.255636$...
    >
    > For Example, If I delete two times like the below code, the program will
    > behave differently. is this correct?


    It is undefined behavior.

    > As C++ FAQ the delete checks where or not f1 has memory. How can the
    > following program can go wrong?


    The FAQ doesn't say that, and your program is wrong.

    > [16.7] Do I need to check for NULL before delete p?
    >
    > No!
    >
    > The C++ language guarantees that delete p will do nothing if p is equal to
    > NULL. Since you might get the test backwards, and since most testing
    > methodologies force you to explicitly test every branch point, you should
    > not put in the redundant if test.


    Delete only checks to see if the value passed in is NULL. Otherwise it
    needs to be a previous new'd value (not already deleted).

    In your case you feed the same, non-NULL value to delete twice.
    This is undefined behavior. Don't do it.

    If you had set f1 to null

    int main{
    foo *f1 = new foo()
    delete f1;
    f1 = 0;
    delete f1;
    return (0);
    }

    Then it would have been ok (the latter delete would be a no op).
    Ron Natalie, Jul 22, 2003
    #2
    1. Advertising

  3. "sam" <> wrote in message
    news:eek:lcTa.255636$...
    >
    > For Example, If I delete two times like the below code, the program will
    > behave differently. is this correct?
    > As C++ FAQ the delete checks where or not f1 has memory. How can the
    > following program can go wrong?
    >
    > class foo{
    > //
    > };
    > int main{
    > foo *f1 = new foo()
    > delete f1;
    > delete f1;
    > return (0);
    > }



    When you delete a pointer, the pointer itself is not changed - it still
    points where it used to, but what it points at isn't there any more.

    In your code, you free the foo object pointed to by f1, and then try to free
    the object again. Since the object is no longer there, anything might
    happen.


    >
    >
    > C++ FAQ
    > [16.7] Do I need to check for NULL before delete p?
    >
    > No!
    >
    > The C++ language guarantees that delete p will do nothing if p is equal to
    > NULL. Since you might get the test backwards, and since most testing
    > methodologies force you to explicitly test every branch point, you should
    > not put in the redundant if test.
    >
    > Wrong:
    >
    > if (p != NULL)
    > delete p;
    >
    > Right:
    >
    > delete p;


    Paul
    Paul Thompson, Jul 22, 2003
    #3
    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. Replies:
    5
    Views:
    26,613
    Mike Schilling
    Mar 29, 2006
  2. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,823
    Mathieu Malaterre
    Aug 17, 2004
  3. G Fernandes
    Replies:
    9
    Views:
    590
    DHOLLINGSWORTH2
    Feb 27, 2005
  4. Mike
    Replies:
    12
    Views:
    712
    Daniel Pitts
    Sep 26, 2008
  5. Poster Matt

    Check for NULL before calling free()?

    Poster Matt, Feb 1, 2010, in forum: C Programming
    Replies:
    45
    Views:
    4,772
    Tim Rentsch
    Feb 6, 2010
Loading...

Share This Page