__attribute__ aligned could be more efficient
Mason
slash.tmp@free.fr
Sun Mar 29 16:56:00 GMT 2015
Hello everyone,
Consider the following program.
#include <stdio.h>
#include <stddef.h>
struct foo1 { char s[80]; };
struct foo2 { char s[80]; } __attribute__ ((aligned (64)));
struct bar1 { struct foo1 f; int i; };
struct bar2 { struct foo2 f; int i; };
#define P(arg) printf("sizeof(" #arg ") = %u\n", (unsigned)sizeof(arg))
int main(void)
{
P(struct foo1);
P(struct foo2);
P(struct bar1); printf("offset=%u\n", (unsigned)offsetof(struct bar1, i));
P(struct bar2); printf("offset=%u\n", (unsigned)offsetof(struct bar2, i));
return 0;
}
$ gcc -O3 -Wall align.c
$ ./a.out
sizeof(struct foo1) = 80
sizeof(struct foo2) = 128
sizeof(struct bar1) = 84
offset=80
sizeof(struct bar2) = 192
offset=128
I didn't expect sizeof(struct bar2) to be 192.
NB: I first sent this message to the wrong mailing list, where
Andreas pointed out that the "aligned" attribute effectively
changes the *size* of bar2.
In my real-life scenario, foo2 is typically used to define a
static object of type foo2, and the padding is expected.
But in some cases, we want to define a super-struct like bar2,
where the whole struct is 64-byte aligned, not the inner struct.
So how do we drop / cancel / override the alignment request
for foo2 in the bar2 definition? This doesn't work:
struct bar3 {
struct foo2 f __attribute__ ((aligned (4)));
int i;
} __attribute__ ((aligned (64)));
Do I have to duplicate the definition for struct foo2, one with
the align attribute, one without?
Regards.
More information about the Gcc-help
mailing list