wrong struct alignment

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 1 13:13:01 PDT 2015


On Wednesday, 1 July 2015 at 20:01:08 UTC, dd0s wrote:
>     int    scope_id; // 4
>     short  port; // 8 // <-- this is 4 bytes instead of 2

That's expected because scope_id takes 4 bytes, so port has to be 
at 8 so it doesn't overlap the preceding int.


The extra two bytes you see in sizeof are probably at the *end* 
of the struct, not between the members.

To get rid of them, out align(2) on the outside too:

align(2)
struct yourthing {
   align(2):
     members
}


That will trim the size. The align inside the struct packs the 
members, but still pads the end (which means an array of these 
structs will be aligned on the word boundary). The align on the 
outside removes the padding at the end, meaning an an array of 
the structs would be packed too.


More information about the Digitalmars-d-learn mailing list