Cast alignment warnings

Alexander Bartolich alexander.bartolich at gmx.at
Mon Aug 1 19:44:48 UTC 2011


Julien ÉLIE wrote:
> [...]
> On a Debian sparc64 server (TI UltraSparc IIe Hummingbird), I encounter cast alignment errors:
>
> /home/iulius/autobuild/bin/gcc-core-4.6.1/bin/gcc -g -O2 -DDEBUG=1 -Wall -W -Wendif-labels -Wpointer-arith -Wbad-function-cast -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Werror -g -O2 -I../include  -c network.c

This is an error only because compiler options -Wcast-align and -Werror make it so.

>[...]
> I admit I am a bit puzzled.
> How are such alignment errors supposed to be fixed?

On RISC machines data types must be naturally aligned. For example
dereferencing a pointer to int will segfault if the pointer value
is not a multiple of sizeof(int). To fix this kind of errors you need
to either correct the pointer value, e.g. by inserting padding bytes.
Or you make use of GCC's __attribute__((packed)) in the declaration.

The problem at hand is of a completely different kind, though. Here
you just have an overzealous compiler that cannot guarantee at compile
time that no exception will occur at run-time.

Workarounds:
- use union instead of casts
- use getaddrinfo and getnameinfo

Ciao

     Alexander.



More information about the inn-workers mailing list