【发布时间】:2013-07-04 06:52:10
【问题描述】:
考虑以下几点:
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。大小(A)==1,大小(B)==4。为什么会这样?我需要结构 B 之类的大小为 1。
【问题讨论】:
-
FWIW,GCC 4.7.2 看起来不错:ideone.com/5UpazC(这是直接应用于内部结构的属性)。
-
@OliCharlesworth 您的代码与 OP 的不同。见ideone.com/Z4wOkE
-
@johnchen902:我知道,但是 OP 说他也尝试将属性直接应用于内部结构。
-
类似的东西除了工会似乎是这里接受的答案的情况,虽然没有解释:stackoverflow.com/questions/12383708/…
标签: c++ bit-fields