Versions Compared

Key

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

Different alignments are possible for different types of objects. If the type-checking system is overridden by an explicit cast or the pointer is converted to a void pointer (void *) and then to a different type, the alignment of an object may be changed.

According to C11, Section 6.3.2.3, para. 7 of the C standard [ISO/IEC 9899:2011], states:

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.

...

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.

Noncompliant Code Example

For objects declared on the stack, The the C Standard standard [ISO/IEC 9899:2011] provides alignas to declare an object to have a stricter alignment. This  It can be used to resolve the following noncompliant code example.

...

CERT C++ Secure Coding Standard: EXP36-CPP. Do not convert pointers into more strictly aligned pointer types

ISO/IEC 9899:2011 Section  Section 6.2.5, "Types"

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

...