Most of the time I see some C++ programmers who check if a pointer is NULL before deleting it.
if (ptr != NULL) {
delete ptr;
ptr = NULL;
}
Well, according to C++03 [ISO/IEC IS 14882:2003] §5.3.5/2 which explicitly states:
…if the value of the operand of delete is the null pointer the operation has no effect.
Therefore, deleting a NULL pointer has no effect (if the deallocation function is one supplied in the standard library), so it is not necessary to check for a NULL pointer before calling delete.
delete ptr;
ptr = NULL;
- Note: Keep in mind that deleting a void* pointer results in
undefined behaviorwhether it’s NULL or not.
See also
- Building Unreal Engine 5.6 From the GitHub Source Code on GNU/Linux With Android Support
- Rust Devs Think We’re Hopeless; Let’s Prove Them Wrong (with C++ Memory Leaks)!
- Building Unreal Engine 5.6 From the GitHub Source Code on Microsoft Windows
- A quick workaround for Unreal Engine Modeling Tools Editor Mode plugin not showing up on Linux and macOS