Using __attribute((packed))__ on picky architectures

Ian Lance Taylor iant@google.com
Fri Jan 16 17:00:00 GMT 2009


"Darren Rook" <route66@gmail.com> writes:

> On Fri, Jan 16, 2009 at 10:33 AM, Ian Lance Taylor <iant@google.com> wrote:
>
>>> Is there some method to tell the compiler that *val may not be word
>>> aligned, so the compiler can generate code to work around the
>>> alignment issue (instead of assuming the *val will be word aligned)?
>>
>> No.  Avoid writing this sort of code.
>
> I thought as much.
>
> Then why is attribute((packed)) provided?  I really don't see a use
> for it.  Why not rename it to __attribute__((addressviolation))?

The potential address violation only occurs when you take the address
of a field and pass it to another function.  There are many ways to
use structures without taking the address of a field.

There are two main reasons to use __attribute__ ((packed)).  The first
is to minimize the amount of memory used by a data structure,
typically when using the struct in a large array.  The second is to
make the struct mimic a data layout which is imposed externally, such
as when accessing memory mapped hardware.

Ian



More information about the Gcc-help mailing list