Subject: Re: CVS commit: src/sys/dev/scsipi
To: Christian Biere <christianbiere@gmx.de>
From: None <khym@azeotrope.org>
List: source-changes
Date: 12/01/2006 14:28:08
On Fri, Dec 01, 2006 at 08:36:23PM +0100, Christian Biere wrote:
> I suggest using STATIC_ASSERT(sizeof(struct whatever) == whatever) wherever
> size matters.
Interesting... I didn't think that the aligned attribute would change
the sizeof the struct, but it does :( The gcc documentation (info gcc)
doesn't seem to make any mention of that (although I suppose if you set
the attribute on a structure member, it'd obviously have to increase
the sizeof the struct. I didn't think it'd do it when aligning the
struct itself.)
So how does one ensure that the start of the struct is aligned to a
32-bit boundary, but the sizeof the struct stays unchanged?
In an off-list email, Martin Husemann had suggested using a union to
force the appropriate alignment, but it looks like that doesn't work:
#include <stdio.h>
struct odd {
char x[3];
} __attribute__((__packed__));
struct evenunion {
union {
int i_x;
char x[4];
} u_x;
char y[4];
} __attribute__((__packed__));
int main(void)
{
struct odd o;
struct evenunion e;
printf("odd is at %p, size is %d\n", &o, sizeof(o));
printf("evenunion is at %p, size is %d\n", &e, sizeof(e));
return 0;
}
odd is at 0x1ffffe638, size is 3
evenunion is at 0x1ffffe63b, size is 8
However, removing the __packed__ attribute from struct evenunion does
make it work. Is __packed__ necessary when all the members are chars?