union.sizeof

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 25 15:45:22 PDT 2017


zabruk70 wrote:

> //DMD 2.073.1 and latest 2.075.0-master-972eaed
> //Windows 7 32-bit
>
> union Union1
> {
>    align(1):
>    byte[5] bytes5;
>    struct
>    {
>      align(1):
>      char char1;
>      uint int1;
>    }
> }
>
> void main ()
> {
>    import std.stdio: writefln;
>    writefln("Union1.sizeof=%d", Union1.sizeof);  //prints 8, not 5
> }
>
> I expect size of Union1 is 5 (5 bytes == char + uint == 5).
> Is this my bug or DMD?

`align(1) union Union1` will do the trick.

what you did is members packing. but the union itself is padded to integer 
size too. i.e. internal `align` will set aligning for *members*, and 
external `align` will change padding of the whole thing.


More information about the Digitalmars-d-learn mailing list