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
- A quick workaround for Unreal Engine Modeling Tools Editor Mode plugin not showing up on Linux and macOS
- Host Unreal Engine 4 projects on Microsoft Azure DevOPS with unlimited cost-free Git LFS quota
- Gregorian / Jalali (a.k.a. Persian Calendar) Date Conversion in C++ using boost::locale
- The long awaited FreeBSD www/wt and www/wt3 ports updates