...
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 | ||||||
---|---|---|---|---|---|---|---|---|---|
| 94 S | Fully implemented | |||||||
GCC |
| Can detect some violations of this rule when the | |||||||
EDG | |||||||||
Compass/ROSE | Can detect violations of this rule. However, it does not flag explicit casts to | ||||||||
| castexpr | Fully implemented. | |||||||
PRQA QA-C |
| 3305 | Fully implemented |
Noncompliant Code Example
...
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]
...