Si consideri il seguente:C++ sottostruttura bitfield Dimensioni
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));
compilatore g ++ è 4.8.1. sizeof (A) == 1, sizeof (B) == 4. Perché è così? Ho bisogno di qualcosa come la struttura B per avere la dimensione 1.
FWIW, sembra ok con GCC 4.7.2: http://ideone.com/5UpazC (questo è con l'attributo applicato direttamente alla struttura interna). –
http://coliru.stacked-crooked.com/view?id=93e976a41246e1de4cdae7418040122e-f674c1a6d04c632b71a62362c0ccfc51 –
@OliCharlesworth Il tuo codice è diverso da OP. Vedi http://ideone.com/Z4wOkE – johnchen902