sizeof(union) and #pragma pack()

Dave Trollope, Diane Barrowman daveanddiane@kringlecottage.com
Wed Sep 22 10:00:00 GMT 2004


Hi,

The following code seems to cause the sizeof() function to return a size 
that is two bytes larger than I expected. Is this the right behaviour?

struct a6 { unsigned long a; unsigned short b };
#pragma pack(2)
struct a10 {
union {
    struct a6 emedded;
    int *ptr;
}
unsigned long junk;
};
#pragma pack()

I was expecting sizeof(struct a10) to return 10 but it returns 12.

If I move the #pragma pack(2) above the first struct definition, 
sizeof(struct a10) returns 10 as expected. This is presumably because 
the packing between the structure and the union is miscalculated from 
the structure defaulting to a padded 8.

I'd like to know if this is the expected behaviour, or if this is 
something that needs fixing?

Cheers
Dave

-- 
Dave, Diane, Kringle & Baby
http://www.geocities.com/SiliconValley/7499




More information about the Gcc mailing list