Re: Size of structs containing unions



luke wrote:
hi,
in Visula C++ 6.0 I have declared a struct like this:

typedef struct  _WRITE_INPUT {

    ULONG   DeviceNumber;
    ULONG   RegisterNumber;
    union   {
        USHORT  ShortData;
        UCHAR   CharData;
    };
}   WRITE_INPUT;

I can't understand why, sizeof(WRITE_INPUT) returns 12. It should
return 10, shouldn't it?

sizeof(ULONG) = 4
sizeof(ULONG) = 4
sizeof(USHORT) = 2 (longest union field)

4 + 4 + 2 = 10

thanks
bye
luke

c.l.c FAQ #2.13 would answer your question
.