Main Content

MISRA C++:2023 Rule 8.2.6

An object with integral, enumerated, or pointer to void type shall not be cast to a pointer type

Since R2024b

Description

Rule Definition

An object with integral, enumerated, or pointer to void type shall not be cast to a pointer type.

Rationale

Casting void* pointer, integral types, or enumerated types to an object with pointer type results in unspecified behavior.

Polyspace Implementation

The checker allows an exception on zero constants, such as 0x0, 0, or 0U.

Polyspace® does not report a violation if a void* pointer is converted to a pointer to function.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

In this example, Polyspace flags the conversion of integers to pointers. Polyspace does not flag converting zero constants such as 0, 0x0, or 0U to pointers. Such conversions are equivalent to zero initialization of a pointer, which has a specified behavior.

void foo ()
{
  void *p1;
  void *p2;
  void *p3;

  p1 = static_cast < void *>(0x0);	//Compliant
  p2 = static_cast < void *>(0U);	//Compliant
  p3 = static_cast < void *>(0);	//Compliant

  p1 = (void *) 0xDEADBEEF;	//Noncompliant
  p2 = (void *) 03;		//Noncompliant
  p3 = (void *) 12345678U;	//Noncompliant
}

Check Information

Group: Expressions
Category: Required

Version History

Introduced in R2024b