Check for NULL before delete in C++ - good practice? [duplicate]
Asked Answered
V

4

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.

Venerate answered 11/12, 2012 at 10:56 Comment(4)
I think that more often than not it is just ignorance. Also, regarding how to delete objects right... use RAII.Nels
#615855Percaline
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.Harve
Consider using smart pointers if possible.Wages
H
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.

Hierarchize answered 11/12, 2012 at 10:59 Comment(1)
+1 for 'ignorance', that's exactly itMease
S
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!

Selfseeker answered 11/12, 2012 at 10:57 Comment(7)
What the heck - giving C a bad name. You can free NULL safely in C...Swampland
what if I overload delete for my class and forget to add safety checks in there?Geopolitics
Then you've provided a broken delete operator implementation. That's not the fault of the user of the operator, but the implementer.Selfseeker
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?Geopolitics
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.Selfseeker
@aleguna yes, it is helpful: fix it where broken.Nels
@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?Turnip
S
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.

Staid answered 11/12, 2012 at 11:0 Comment(0)
J
1

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

Judoka answered 11/12, 2012 at 11:2 Comment(3)
I'm sorry, what is "valgrind"?Venerate
valgrind is a program that checks for memory corruption. It gives a free non heap warning when attempting to free a NULL pointer.Judoka
@Judoka 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.Whitehurst

© 2022 - 2024 — McMap. All rights reserved.