Re: "cast increases required alignment of target type"




DGG wrote:
> Hi,
>
> I am porting some code across hardware platform.
>
> I got the following warning using the cygming compiler

don't know this compiler but I've seen this warning produced
by gcc.

> warning: cast increases required alignment of target type

If the platform you're porting to does not like unaligned
memory accesses (eg, ARM7), then these warnings are worth
analyzing. They can point to some dirty tricks like:

static uint_8 foo[4];

{
// reading 4 bytes into a 32bit variable
uint_32 bar = *((uint_32 *)foo);
}

While this trick works perfectly on some platforms,
it fails on ARM7 if foo[4] is not word-aligned (where word is 32 bits).

On the other hand, the warning doesn't necessarily indicate a problem,
each warning has to be analyzed individually.

> I believe this can, say, cast a byte-aligned pointer to a
> integer-aligned pointer, which can break for stricter processors.

Not sure I understand this. The warning is there to tell you that
you're casting from one type to another and this 'another' type
is larger in size, which might imply a certain alignment requirement
for it. It is then your responsibility as a developer to ensure
that the variable is properly aligned for such a cast or that
there are no alignment restrictions on the given platform or
get rid of this cast and do things in a different way.

.



Relevant Pages