[previous] 3305 [next] Pointer cast to stricter alignment.
Pointers

A pointer is being cast to a pointer type which has a "stricter" alignment requirement.

The alignment of types in memory is implementation-defined. In a typical implementation, it could be a requirement of the architecture that a 4 byte int would always be allocated on a 4 byte boundary in memory while a char could be allocated at any memory address. In such circumstances, it would be safe to cast an "int *" to a "char *", but it would be dangerous to cast a "char *" to an "int *" unless the address happens to be located on a 4 byte boundary

QA·C provides the -align option to specify the implemented alignment constraint for a particular type. Message 3305 will be generated whenever a pointer is being converted to a type with a stricter alignment requirement, i.e. a larger alignment setting.

Default size and alignment settings are as follows:

TYPE SIZE (bits) ALIGNMENT (bytes)
char 8 1
short 16 2
int 32 4
long 32 4
long long 64 8
float 32 4
double 64 8
long double 64 8
code pointer 32 4
data pointer 32 4

For example:


/*PRQA S 310,2017,3210,3408,3447,3625,3631 ++*/

extern char  *pc;               /* one  byte aligned */
extern short *ps;               /* two  byte aligned */
extern int   *pi;               /* four byte aligned */

extern void foo(void)
{
    pi = (int*)pc;              /* Message 3305 */
    pc = (char *)pi;            /* OK */
}

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents