Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting nullptr - performance overhead?

Tags:

c++

Operator delete checks itself if the pointer is nullptr. Is there any performance overhead when calling delete on a nullptr without checking it yourself?

delete ptr;

or

if (ptr != nullptr) delete ptr; 

Which of the above executes faster if ptr is nullptr?

like image 494
NFRCR Avatar asked Mar 16 '12 17:03

NFRCR


1 Answers

As usual, it depends on the compiler.

I use MSVC, which compiles both these lines to exactly the same code.

The rules say that if the pointer is null, the delete has no effect. So if you don't check that, the compiler has to anyway.

like image 67
Bo Persson Avatar answered Sep 17 '22 12:09

Bo Persson