0

There are several questions regarding if to put null check before delete or not. Now, I have still seen such practices in many production code and I don't believe that all those programmers were unaware of the fact that delete 0; is safe.

So, I wonder that isn't it worth assuming that null check before delete/free would provide a performance optimization by saving a function call ?

if(0 != p)
  delete p;
Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Premature pessimization in majority of cases (because usually the pointer is not null, is it?), actually. Anyway, the check will be there nonetheless if compiling with GCC. –  Jun 13 '11 at 13:31
  • `if (0 != p)`: Personal preference, but I find that one of the uglier constructs in use, oftentimes mandatory use (it's in the project coding standards). – David Hammen Jun 13 '11 at 14:03

3 Answers3

6

No. delete is not a function, though there may be an operator delete call behind it depending on the type.

At best you're saving a second comparison to 0. It's all premature optimisation: none of this will be your bottleneck, and you're writing redundant code.

Just write:

delete ptr;
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 3
    I'd call this premature deoptimization rather than premature optimization. Null pointers should be rare, so most of the time that if test will slow the program down by a tiny bit. – David Hammen Jun 13 '11 at 13:59
0

I don't believe that all those programmers were unaware of the fact that delete 0; is safe.

Well, they were. That's pretty much it. It's a fact that delete or free on null is perfectly safe. If they weren't unaware of it, then they wouldn't have wasted time checking for it.

So, I wonder that isn't it worth assuming that null check before delete/free would provide a performance optimization by saving a function call ?

You're making an assumption ... ... because what, you don't want to believe that those guys are wrong? Sorry, buddy- they were wrong, and that's that. You've got no evidence or rational reasons to believe that doing a null check has such a benefit.

Or that whoever wrote that code thought so. After all, even if you could eliminate a function call this way, the cost of the function call here is miniscule, if it's not inlined, which it may well be, and it costs more for the programmer time to write the check, which is already written for you. I'd rather be over-zealous about correctness than prematurely optimize like this- are you genuinely doing anyone a favour by suggesting that it may be an optimization?

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • @Tomalak, I second you. I gave +1 for content of the answer. But language of @DeadMG is really "**Deadly** My God". Bad attitude for fellow learners. – iammilind Jun 13 '11 at 15:01
0

Now, I have still seen such practices in many production code and I don't believe that all those programmers were unaware of the fact that delete 0; is safe.

Long, long ago there was no guarantee that delete (and free) would harmlessly do nothing with a null pointer. Instead they blew up with some compilers / some machines. Hence the origin of the practice.

What keeps this practice alive is a combination of cargo cult programming and small mind mentality ("A foolish consistency is the hobgoblin of small minds.") Cargo cult programmers use cut-and-paste, so when they see some widely-used syntax they copy it. In this case, because some code does that null pointer check, a cargo cult programmer will think that that is the way to do it.

Small mind mentality dictates that there is only one way to do it. Because some decrepit legacy code that no one wants to touch uses this null pointer check, all code must release dynamically-allocated memory this way. I have seen coding standards for multiple projects that mandate this behavior (and those coding standards themselves are created by cargo cult techniques).

David Hammen
  • 32,454
  • 9
  • 60
  • 108
  • "Long, long ago there was no guarantee that delete (and free) would harmlessly do nothing with a null pointer." How long ago? The guarantee is clearly there in ISO C 90 (for `free`), and I seem to recall it in K&R C (1st edition---but my memory isn't always that good, so I could be wrong there). – James Kanze Jun 13 '11 at 14:08
  • @James: K&R didn't go into the details of what `free` was supposed to do with a null pointer. Lots of systems blew up. While the first C standard did come out in 1990, when vendors decided to make their compilers compliant was a different story. Programs continued to blow up with `free(0)` for quite some time after the release of the standard. But not in this millennium as far as I know. – David Hammen Jun 13 '11 at 15:20