This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
__attribute__ aligned could be more efficient
- From: Mason <slash dot tmp at free dot fr>
- To: GCC dev <gcc at gcc dot gnu dot org>
- Date: Sun, 29 Mar 2015 17:19:43 +0200
- Subject: __attribute__ aligned could be more efficient
- Authentication-results: sourceware.org; auth=none
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.
gcc lays out bar2 like this:
foo2(80) padding(48) i(4) padding(60)
But it seems (to me) that gcc could fit the "int" field in the
first padding, to save space:
foo2(80) i(4) padding(44)
Is there a way to "cancel" 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)));
Regards.