Size of structs containing unions



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

.



Relevant Pages

  • Re: question
    ... I have already got it to work by changing some thing in the typedef struct, but i would like to see how the more experienced ... union{int num; char letter;}; ... Either that or terminate it with a new line. ... to validating user input. ...
    (comp.lang.c)
  • Re: C Offsetof
    ... I have following structures and union ... int a; ... union Member *mem; ...
    (comp.lang.c)
  • Re: C Offsetof
    ... I have following structures and union ... int a; ... GetMemFromBuf (myBuff, (void *)myMem); ...
    (comp.lang.c)
  • Re: Is this valid program
    ... int main ... >pointer declared inside union. ...
    (comp.lang.c)
  • Re: C Offsetof
    ... I have following structures and union ... int a; ... union Member *mem; ...
    (comp.lang.c)