Posts: 8
Threads: 5
Joined: May 2014
Reputation:
0
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?
<t></t>
Posts: 35
Threads: 4
Joined: Sep 2004
Reputation:
0
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.
<t></t>
Posts: 632
Threads: 18
Joined: Jan 2006
Reputation:
1
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.
Posted by and on behalf of the MISRA C Working Group