Struct alignment vs alignment of fields

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 7 10:22:14 PDT 2014


(Original discussion: 
http://forum.dlang.org/thread/fckwpddiwxonabqaflqt@forum.dlang.org#post-pskjgieddhpntzaokohj:40forum.dlang.org)

     align(1) struct A
     {
     align(1):
         int qtim;
         int bid;
         int ofr;
         int bidsiz;
         int ofrsiz;
         short mode;
         char[1] ex;
         char[4] mmid;
     }

     align(1) struct B
     {
         int qtim;
         int bid;
         int ofr;
         int bidsiz;
         int ofrsiz;
         short mode;
         char[1] ex;
         char[4] mmid;
     }

I would expect `B` to have a gap between `ex` and `mmid`. AFAIK 
the outer `align(1)` only applies to the struct in its entirety, 
not to the individual fields. However for both DMD git and LDC 
0.14.0-alpha1 (based on DMD 2.065), `A` and `B` have the same 
size.

After some thinking, I believe this is because arrays inherit the 
alignment of their element types. Is this correct? If yes, where 
is this documented? I had expected `char[4]` to be aligned at a 
4-byte boundary.


More information about the Digitalmars-d-learn mailing list