Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type. If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined.

(See also undefined behavior 25  of Annex J.)

If the misaligned pointer is dereferenced, the program may terminate abnormally. The cast alone may cause a loss of information, even if the value is not dereferenced. For example, the assertion in the following code example will fail on some conforming implementations, even though no pointers are dereferenced:

...

Tool

Version

Checker

Description

LDRA tool suite

Include Page
LDRA_V
LDRA_V

94 S
540 S

Fully implemented
GCC
Include Page
GCC_V
GCC_V
 

Can detect some violations of this rule when the -Wcast-align flag is used.

EDG   
Compass/ROSE  

Can detect violations of this rule. However, it does not flag explicit casts to void * and then back to another pointer type.

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

castexpr

Fully implemented.
PRQA QA·CQA-C
Include Page
PRQA_V
PRQA_V
 Fully implemented

...

ISO/IEC TR 17961 (Draft) Converting pointer values to more strictly aligned pointer types [alignconv]

ISO/IEC TR 24772 "HFC Pointer casting and pointer type changes"

MISRA Rules 11.2 and 11.3

Bibliography

Walfridsson, Krister. Aliasing, pointer casts and gcc 3.3. August, 2003.
[Bryant 2003]

...