6

Possible Duplicate:
Is there any reason to check for a NULL pointer before deleting?

I often see the following in code:

if(pointer)
    delete pointer;

To my understanding it is safe to delete a null pointer, so what is the point of this check?

Community
  • 1
  • 1
user987280
  • 1,578
  • 1
  • 14
  • 24
  • 3
    Important point to remember is set it NULL once deleted. – vrrathod Nov 04 '11 at 03:12
  • 1
    To my understanding, your understanding is correct but not all people share it. But let's see what the language laywers say. –  Nov 04 '11 at 03:12
  • 2
    Deleting of a null pointer is absolutely safe in C++, but not all people know about that. It's especially notable in C community, where language guaranties that free(NULL) has no effect, but people don't like read the standards and intuitively it seems that freeing of NULL can harm the system. Thus there're much more examples of if (p) free(p); in C world :) – Dan Kruchinin Nov 04 '11 at 03:18
  • @DanKruchinin, it sounds so counter intuitive, that I personally never went to check the standard for it! But while we're at it, can the same be said about `kfree` and `vfree` in the linux kernel? – Shahbaz Nov 04 '11 at 03:21

4 Answers4

9

delete will check if the pointer is NULL for you, so you're right that the check isn't needed.

You might also see that some people set the pointer to NULL after it's deleted so that you don't do anything stupid like try and use memory that is no longer yours or stop you from deleting the pointer twice, which will cause an error.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
  • To your comment "so that you don't do anything stupid like try and use memory that is no longer yours", even dereferencing a null pointer is a UB. It is helpful in case when the pointer is deleted again (double deletion which you already mentioned) – infinite loop May 30 '17 at 13:56
1

While it is safe now, it wasn't always :-) so it's likely habitual. Also there are other consequences to delete. 1) if you are using a specialized memory manager and overriding new and delete operators, then you may very well have to do a check Operator Delete for more details

Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58
  • 6
    `[citation needed]`. It's a pretty easy thing to add, and IIRC C has guaranteed that `free` handles `NULL` correctly since forever (at least C89, I wouldn't be surprised if it was in ANSI C too). So I find it hard to believe that there ever was a usable C++ implementation that couldn't handle `delete NULL;`. –  Nov 04 '11 at 03:17
1

The check is not necessary.

The documentation states that delete will "deallocate the memory block pointed by ptr (if not-null)"

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
0

Most people do this because they usually have error handling otherwise (other than that I don't really see the point of doing the check). In some instances they do it to check that they are freeing something and not accidentally changing a pointer somewhere and causing a memory leak by not freeing it. free(NULL); should work in all cases and not error out so unless there's error handling involved you can remove the if statement and just do the free.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88