wrong struct alignment

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 1 13:15:38 PDT 2015


On Wednesday, 1 July 2015 at 20:01:08 UTC, dd0s wrote:
> i have the following struct, and i expect it to have 30 bytes
> but sizeof tells me it has 32 bytes. dmd seems to still use 
> 4byte alignment altough i specified to align 2bytes.
>
> struct netadr_t {
> 	align(2):
>     int    type; // 0
>     int    scope_id; // 4
>     short  port; // 8 // <-- this is 4 bytes instead of 2
>     int    sock;   // 10
>
>     union {
>     ubyte[4]    ip; // 14
>     ubyte[10] ipx;
>     ubyte[16] ip6;
>     }
> }
>
> since i'm interfacing with a c library the struct layout has to 
> be equal :(

Disclaimer: My understanding of all things alignment is limited.

`pragma(msg, netadr_t.sock.offsetof);` prints "10LU", so port 
seems to really only take 2 bytes. The struct itself has padding 
at the end. You can eliminate that with an `align(1)` on the 
struct:
----
align(1) struct netadr_t {
     align(2):
     ...
}
----


More information about the Digitalmars-d-learn mailing list