Why is this an issue?
This is a draft version of a MISRA C++ 202x rule proposed for public review.
MISRA Rule 7.11.1
Analysis Type: Decidable,Single Translation Unit
Using any integral literal representing zero, including the literal
0 or the macro
NULL, to represent the
null-pointer-constant is a violation of this rule.
In addition, the macro
NULL shall not be used in any other context.
The C++ Standard defines the object
nullptr as the null-pointer-constant.
0 can also be used to represent a null-pointer-constant. However,
0 has type
its use can lead to unexpected overload resolution. Note that the macro
NULL expands to
Note: some library functions provide overloads for
std::nullptr_t so that they can be selected during overload
resolution at compile-time, avoiding the need for a run-time check against
void f1( int32_t * );
f1( nullptr ); // Compliant
f1( 0 ); // Non-compliant - 0 used as the null pointer constant
The following example shows the selection of an integer overload when
NULL (which has a value of
0) is used instead of
void f3( int32_t );
void f3( int32_t * );
f3( NULL ); // Non-compliant - calls the int32_t overload
f3( nullptr ); // Compliant - calls the int32_t * overload
The following example shows non-compliant uses of
NULL, where it is not used as the null-pointer-constant:
#define MYNULL NULL // Non-compliant
int32_t one = NULL + 1; // Non-compliant - NULL used as an integer
throw NULL; // Non-compliant - caught by catch ( int )
Copyright The MISRA Consortium Limited © 2023
S4962 targets the same defect as this rule but for a non-mission-critical context.
External coding guidelines
C++ Core Guidelines
ES.47 - Use nullptr rather than 0 or NULL