6

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

I know that The C++ language guarantees that delete p will do nothing if p is equal to NULL. But constantly in different projects, articles, examples I see that it is checking for NULL before delete. Usually in format

    if(pObj)
       delete pObj;

Why is it so? Some historical reasons? I'm totally confused about how to do delete objects right.

Community
  • 1
  • 1
HelloHi
  • 165
  • 2
  • 10
  • 13
    I think that more often than not it is just ignorance. Also, regarding how to delete objects right... use RAII. – R. Martinho Fernandes Dec 11 '12 at 10:56
  • http://stackoverflow.com/questions/615355/is-there-any-reason-to-check-for-a-null-pointer-before-deleting – pbhd Dec 11 '12 at 11:01
  • There are platform where delete does not perform as the standard one. Embedded particullary. On one platform delete(NULL) was asserting. This is to optimize and think the application different. But I've saw that some have used this "hack" and didn't think about object lifetime... resulting in larger & slower application. – joy Dec 11 '12 at 11:02
  • 2
    Consider using smart pointers if possible. – Baz Dec 11 '12 at 11:25

4 Answers4

15

Why is it so?

Ignorance. Some people do not know that delete(NULL); is not doing anything.

You can not really check if the pointer is really valid. If you delete twice, you are invoking an undefined behavior.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
11

No this is completely pointless. delete will not delete a pointer that is already set to null! So delete a null pointer all your like!

goji
  • 6,911
  • 3
  • 42
  • 59
  • 5
    What the heck - giving C a bad name. You can `free` `NULL` safely in C... – Mike Kwan Dec 11 '12 at 11:01
  • what if I overload `delete` for my class and forget to add safety checks in there? –  Dec 11 '12 at 11:05
  • Then you've provided a broken delete operator implementation. That's not the fault of the user of the operator, but the implementer. – goji Dec 11 '12 at 11:06
  • Knowing that is not helpful when you've got an entire production in ruins because some lazy bastard didn't care to check for `NULL` before deleting, is it? –  Dec 11 '12 at 11:10
  • It's perfectly normal c++ to not check for null before delete'ing. Nothing lazy about it. If there is problems in the code triggered by not checking null before delete'ing, than the problems are actually elsewhere. – goji Dec 11 '12 at 11:13
  • 1
    @aleguna yes, it is helpful: fix it where broken. – R. Martinho Fernandes Dec 11 '12 at 11:14
  • 3
    @aleguna: The trouble with that logic is that it applies to *everything*. Should your code check that `x++` adds one to `x`? What if someone overloaded `operator++()` with broken code? – j_random_hacker Dec 11 '12 at 11:33
1

delete is an operator and it invokes a destructor. When the delete operator is used with NULL nothing happens, so same as all the answers already it is pointless to check for null.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
1

Another reason to do it is to get rid of the valgrind warnings.

Minion91
  • 1,911
  • 12
  • 19
  • I'm sorry, what is "valgrind"? – HelloHi Dec 11 '12 at 11:03
  • valgrind is a program that checks for memory corruption. It gives a free non heap warning when attempting to free a NULL pointer. – Minion91 Dec 11 '12 at 11:06
  • @Minion91 Doesn't it indicate that problem is elsewhere. Why would someone delete a pointer twice at the first place. it is sort of covering up rather fixing the real problem. – irsis Jun 07 '17 at 04:38