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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With