TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
TIOBE - C Coding Standard
Search

SR:  6.9#1Checked automatically with code checker

Synopsis:Pointers must not be cast to have potentially stricter alignment.
Language:C
Severity Level:2
Category:CONVERSIONS


Description:
Consider the following where a receiving pointer represents a type which has potentially stricter memory alignment criteria than that of the type being cast.

Example:

...
short * s; /* 2 byte aligned */
int * i; /* 4 byte aligned */
...
i = (int*)s; /* potentially mis-aligned */
...
If, for a particular architecture, 'short' and 'int' were aligned as stated above then it is possible for 's' to have an address which does not represent a legal 'int' address. In other words, if the address contained in 's' was accessed as if it represented the location of an 'int' type then it is quite possible that an address exception would be generated.Note that casts which involve a strictly aligned type being converted to a less strict type and back again are guaranteed safe by the ANSI standard.