public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/94338] New: struct member alignment is not carried over to alignment of struct variable
@ 2020-03-26  8:51 huaixin.chx@alibaba-inc.com
  2020-03-26  8:58 ` [Bug c/94338] " huaixin.chx@alibaba-inc.com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: huaixin.chx@alibaba-inc.com @ 2020-03-26  8:51 UTC (permalink / raw)
  To: gcc-bugs

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?

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-03-27 23:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-26  8:51 [Bug c/94338] New: struct member alignment is not carried over to alignment of struct variable huaixin.chx@alibaba-inc.com
2020-03-26  8:58 ` [Bug c/94338] " huaixin.chx@alibaba-inc.com
2020-03-26  9:20 ` rguenth at gcc dot gnu.org
2020-03-26 17:50 ` joseph at codesourcery dot com
2020-03-26 19:50 ` msebor at gcc dot gnu.org
2020-03-27  9:51 ` huaixin.chx@alibaba-inc.com
2020-03-27  9:57 ` huaixin.chx@alibaba-inc.com
2020-03-27 10:17 ` huaixin.chx@alibaba-inc.com
2020-03-27 10:32 ` rguenth at gcc dot gnu.org
2020-03-27 10:38 ` rguenth at gcc dot gnu.org
2020-03-27 13:36 ` huaixin.chx@alibaba-inc.com
2020-03-27 23:36 ` huaixin.chx@alibaba-inc.com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).