MISRA Discussion Forums
"null pointer constant"? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA C:2012 and MISRA C:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=21)
+---- Forum: 8.11 Pointer type conversions (https://forum.misra.org.uk/forumdisplay.php?fid=166)
+---- Thread: "null pointer constant"? (/showthread.php?tid=1324)



"null pointer constant"? - swestin - 08-03-2017

The MISRA C:2012 guidelines (p. 93) define a "null pointer constant" as

the value 0, optionally cast to void *

Does this allow floating-point zero as a null pointer constant, e. g.

int32_t * pint = (void*)0.0f;

That seems crazy to me, but Rule 11.4 specifically allows a "null pointer constant that has integer type", whereas other mentions (Rules 11.1 and 11.2) omit "integer".

Was the intent to specify an integer zero?


Re: "null pointer constant"? - Steve Montgomery - 09-03-2017

The term null pointer constant doesn't appear in the Glossary of the MISRA C Guidelines so its definition is the same as that used in The Standard. I suspect that the description used on p93 omitted the words "an integer constant expression with" for sake of brevity.

I think (void *)0.0f isn't a null pointer constant because it's neither in itself an integer constant expression, nor is it an integer constant expression cast to void *.

As an aside, since casts from arithmetic types to integer types are permitted to appear in integer constant expressions according to The Standard (C99 Section 6.6 para 6) and since floating point constants are explicitly permitted in integer constant expressions when used as operands of a cast (C99 Section 6.6 para 5), I think that (void*)(int)0.0f is a valid null pointer constant.


Re: "null pointer constant"? - misra-c - 31-03-2017

Section 8.11 on page 93 does not intend to gve a precise definition of the C terms, but it is rather included to remind the reader of how C treats pointer conversion.

The MISRA-C working group agrees with the response from Steve Montgomery that (void*)0.0f is not a null pointer constant.

Rule 11.4 is only applicable for conversions between pointer to object and INTEGER type. Only null pointer constants that are integers are covered by this rule as mentioned in the exception. Null pointer constants that have a pointer type are not covered by this rule as they are pointer to incomplete type.

Rule 11.5 is only applicable for conversions from pointer to void to pointer to object. Only null pointer constants that are pointer to void are covered by this rule as mentioned in the exception.

Rule 11.1 is only applicable for conversions between pointer to function and ANY OTHER type. Thus it is applicable to both the integer and pointer forms of the null pointer constant.

Rule 11.2 is only applicable for conversions between pointer to incomplete type and ANY OTHER type. Thus it is applicable to both the integer and pointer forms of the null pointer constant.