MISRA.LITERAL.NULL.PTR.CONST.2012The macro NULL is the only permitted form of integer null pointer constant MISRA C 2012 Rule 11.9: The macro NULL shall be the only permitted form of integer null pointer constantCategory: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationAn integer constant expression with the value 0 shall be derived from expansion of the macro NULL if it appears in any of the following contexts:
Ignoring whitespace and any surrounding parentheses, any such integer constant expression shall represent the entire expansion of NULL. Note: a null pointer constant of the form (void *)0 is permitted, whether or not it was expanded from NULL. RationaleUsing NULL rather than 0 makes it clear that a null pointer constant was intended. ExampleIn the following example, the initialization of p2 is compliant because the integer constant expression 0 does not appear in one of the contexts prohibited by this rule. int32_t *p1 = 0; /* Non-compliant */ int32_t *p2 = ( void * ) 0; /* Compliant */ In the following example, the comparison between p2 and (void *)0 is compliant because the integer constant expression 0 appears as the operand of a cast and not in one of the contexts prohibited by this rule. #define MY_NULL_1 0 #define MY_NULL_2 ( void * ) 0 if ( p1 == MY_NULL_1 ) /* Non-compliant */ { } if ( p2 == MY_NULL_2 ) /* Compliant */ { } The following example is compliant because use of the macro NULL provided by the implementation is always permitted, even if it expands to an integer constant expression with value 0. /* Could also be stdio.h, stdlib.h and others */ #include <stddef.h> extern void f ( uint8_t *p ); /* Compliant for any conforming definition of NULL, such as: * 0 * ( void * ) 0 * ( ( ( 0 ) ) ) * ( ( ( 1 - 1 ) ) ) */ f ( NULL ); See alsoRule 11.4 |