When I am checking whether a pointer is nullptr
in my code, I would like to use if (a == nullptr)
since it make more sense to me. However, I see most senior programmers and tutorial codes like to use if(!a)
instead. I know what it means, but this way is just not as straight-forward as the former one for me. I just wonder, will these two ways of if
checking cause big a runtime difference in real world projects, like when we need to use this if
statement thousands of times?
Asked
Active
Viewed 51 times
2

Remy Lebeau
- 555,201
- 31
- 458
- 770

Tim Chen
- 55
- 4
-
Alternative duplicates: https://stackoverflow.com/questions/60365127/c-difference-between-pointer-and-pointer-nullptr and https://stackoverflow.com/questions/41180094/c-what-is-the-most-efficient-way-to-test-null-pointer – user17732522 Apr 02 '22 at 12:27
-
2Only the second of @user17732522 's suggestions addresses this question (the run-time impact), so I've updated the link give in the close reason. – Adrian McCarthy Apr 02 '22 at 15:09
-
the generate the same code. Its a matter of style choice, in some places the code reads better one way as opposed to the other – pm100 Apr 02 '22 at 17:48