考虑以下几点:C++子结构的位域大小
class A { public:
int gate_type : 4;
bool storage_elem : 1;
uint8_t privilege : 2;
bool present : 1;
} __attribute__((packed));
class B { public:
struct Sub {
int gate_type : 4;
bool storage_elem : 1;
uint8_t privilege : 2;
bool present : 1;
} type_attr; //Also tried with "__attribute__((packed))" here in addition to outside
} __attribute__((packed));
编译为g ++ 4.8.1。 sizeof(A)== 1,sizeof(B)== 4。这是为什么?我需要类似于结构B的东西,其大小为1.
FWIW,它看起来OK GCC 4.7.2:http://ideone.com/5UpazC(这是与直接应用于内部结构的属性)。 –
http://coliru.stacked-crooked.com/view?id=93e976a41246e1de4cdae7418040122e-f674c1a6d04c632b71a62362c0ccfc51 –
@OliCharlesworth您的代码与OP的不同。见http://ideone.com/Z4wOkE – johnchen902