"cast increases required alignment of target type"

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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


Re: "cast increases required alignment of target type"

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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.


Re: "cast increases required alignment of target type"
Quoted text here. Click to load it

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.

Quoted text here. Click to load it


--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
We've slightly trimmed the long signature. Click to see the full one.

Site Timeline