[Bug c/94338] New: struct member alignment is not carried over to alignment of struct variable
huaixin.chx@alibaba-inc.com
gcc-bugzilla@gcc.gnu.org
Thu Mar 26 08:51:13 GMT 2020
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94338
Bug ID: 94338
Summary: struct member alignment is not carried over to
alignment of struct variable
Product: gcc
Version: lto
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: huaixin.chx@alibaba-inc.com
Target Milestone: ---
Document says that “alignment of any given struct or union type is required by
the ISO C standard to be at least a perfect multiple of the lowest common
multiple of the alignments of all of the members of the struct or union in
question.” When the alignment requirement of struct variable is smaller than
the alignment requirement of its member variable, the compiler's behavior is
not specified.
Here is the code.
--------------------------------
#include <stdio.h>
struct {
long a __attribute__((__aligned__(128)));
long b __attribute__((__aligned__(128)));
} A __attribute__((__aligned__(4)));
struct {
int a;
} B;
int main()
{
printf("address of A %lx\n", &A);
printf("address of A.a %lx\n", &A.a);
printf("address of A.b %lx\n", &A.b);
printf("address of B %lx\n", &B);
printf("address of B.a %lx\n", &B.a);
return 0;
}
--------------------------------
And it goes like this.
$gcc align.c -o align
$./align
address of A 42003c
address of A.a 42003c
address of A.b 4200bc
address of B 420038
address of B.a 420038
--------------------------------
Member a and b of struct variable A are not aligned to 128-byte boundary. I
wonder if it is the users' responsible not to write like this. Or is it the
compiler's responsible to align A.a and A.b to 128-byte boundary?
More information about the Gcc-bugs
mailing list