"cast increases required alignment of target type"

Sep 29, 2005 2 Replies

Hi,



I am porting some code across hardware platform.



I got the following warning using the cygming compiler



warning: cast increases required alignment of target type



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



Does anyone have experience in casting around byte-alignments and can give me some suggestion/pointer?



Thanks



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

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.

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.

The usual advice. Don't do it. Cast are indications of bad code. 1) Casting (sometype *) to (othertype *) is especially evil. Get rid of the casts at the original platform. Retest. Then port.

1) Not always. The casts required by incompetent managers to suppress Sensible Warnings are indicative of incompetent managers. The resulting bad code then is more of a symptom.

--

Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- like all pyramid schemes -- ultimately falters. albert@spenarnc.xs4all.nl http://home.hccnet.nl/a.w.m.van.der.horst

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required