You *can* delete a NULL pointer!

This is an often asked question in newsgroups and forums.

A lot of people do this :-

if(p)
    delete p;

The assumption is that, if they do not check for NULL, delete will result in a crash or in random behavior. This is not correct. C++ guarantees that a delete will do nothing if the pointer is NULL. See entry [16.8] on http://www.parashift.com/c++-faq-lite/freestore-mgmt.html.

It’s the same for managed code. You do not have to check for nullptr. Consider the following code :

int main()
{
    R^ r = nullptr;
    delete r;
}

The generated IL checks to see if the object supports IDisposable, and if so, it calls Dispose on it. And if your object implements IDisposable, which in C++/CLI means that you have a destructor, there will be a Dispose generated for you. Note that, Dispose will not be called if the object is a nullptr.

So, in summary, irrespective of whether you are using native objects or CLI objects, you *can* delete a NULL pointer.

Advertisements

8 thoughts on “You *can* delete a NULL pointer!

  1. Nice one!!!
    the pointer deletion always consists of two lines of code. the new change is really good.
    Nish let me ask something more about pointer.

    What about following code

    int *ptr; // not initialized

    delete []ptr; //or delete []ptr;

    what will happen in this case?

    one more
    int *ptr = new int[10];
    delete []ptr;
    //once again im calling delete
    delete []ptr; what will happen in this case? still we need to apply some common programming logic like after deleting the pointer, set it to NULL?

  2. Sarath- uuuuuuuhhhh yeah.
    While the standard dictates that you can delete a null pointer with no ill side effects, if you pass something other than NULL to delete or delete[]- those functions will attempt operate on that value, even if means letting the smoke out of all the chips on your motherboard.

    IOW yes, you have to set the pointer to NULL after calling delete if you plan on randomly passing that pointer to delete again. In fact it’s probably not a bad idea to get in the habit of NULL’ing your pointers afetr deleting them unless you are in a destructor or exiting a function (in which case you might want to consider the oft-maligned auto_ptr for non-array pointers- it just depends on how you’re using it. That’s the second law of smart pointers)
    Laws of Smart pointers:
    1. Smart pointers aren’t
    2. If you don’t know what they do and don’t do, then don’t use them

    (The laws are of course, tounge in cheek. Used properly, smart pointers can make a world of difference. The coming additions in TR1 should be a boon once the community knowledge has embraced them)

  3. Thanks alot for a reply.
    I just wanted to know when deleting a pointer, the new version of delete will search for “IsValidWritePtr”(from C++ point of view).

  4. Chris, one more question

    In this case
    int *ptr = new int[10];
    delete []ptr;
    //once again im calling delete without set the pointer to NULL
    delete []ptr;

    So any error will occur at run time or compile time?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s