Re: Subtleties of __attribute__((packed))

From: David Miller
Date: Wed Dec 06 2006 - 13:05:37 EST


From: Russell King <rmk+lkml@xxxxxxxxxxxxxxxx>
Date: Wed, 6 Dec 2006 17:54:23 +0000

> It does not say "and as such the struct may be aligned to any alignment".

Consider the implication for arrays and pointer arithmetic, it's just
a logical consequence, that's all. It's why the alignment cannot be
assumed for packed structures.

If you have, for example:

struct example {
char b;
short c;
} __attribute__((packed));

And I give you:

extern void foo(struct example *p);

and go:

foo(p + 1);

It is clear that the compiler must assume that all instances
of a packed structure are not necessarily aligned properly.

Even if "p" is aligned, "p + 1" definitely won't be. And this
goes for any array indexing of the given packed structure.

That's why every pointer to such a struct must be assumed to be
unaligned in these cases.

So even though the documentation may not say this explicitly, it's an
implicit logical side effect of packed structures.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/