09-29-2010 11:38 AM
I'm trying to use a bit field as part of a larger structure to map out some data returned by another device. I'm using the #pragma pack(1) command to ensure that no padding is added to the structure. However, it appears that the bit field structure adds padding anyway. Consider this exmaple:
#pragma pack(push, 1)
struct BIT_FIELD
{
unsigned int Bits: 8;
};
struct CHAR
{
unsigned char Char;
};
int x = sizeof(struct BIT_FIELD);
int y = sizeof(struct CHAR);
In this example x is 4 and y is 1. I would expect X to be 1. Is this a bug? Is there a work around?
09-29-2010 04:04 PM
I suppose the reason is that you are mapping bits in an unsigned int: try using an unsigned char instead.
09-29-2010 04:11 PM
I hadn't thought of that. The ISO standard says that bit fields have to be declared as "unisgned int", "signed int", or "int". Nonetheless, that worked! Thanks!