"null pointer constant"?

Moderators: misra-c, david ward

Post Reply
swestin
Posts: 8
Joined: Wed May 28, 2014 6:53 pm
Company: GrammaTech Inc.

"null pointer constant"?

Post by swestin » Wed Mar 08, 2017 11:06 pm

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?

Steve Montgomery
Posts: 104
Joined: Fri Sep 17, 2004 1:31 pm
Company: .
Location: Northumberland or Isle of Skye

Re: "null pointer constant"?

Post by Steve Montgomery » Thu Mar 09, 2017 12:29 pm

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.

misra-c
Posts: 571
Joined: Thu Jan 05, 2006 1:11 pm

Re: "null pointer constant"?

Post by misra-c » Fri Mar 31, 2017 10:00 am

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

Post Reply

Return to “8.11 Pointer type conversions”