我正在Linux上使用C语言中的结构体。我开始使用位域和“packed”属性,但我遇到了奇怪的行为:
struct __attribute__((packed)) {
int a:12;
int b:32;
int c:4;
} t1;
struct __attribute__((packed)) {
int a:12;
int b;
int c:4;
}t2;
void main()
{
printf("%d\n",sizeof(t1)); //output - 6
printf("%d\n",sizeof(t2)); //output - 7
}
这两个结构体完全相同,为什么它们却要占用不同的字节数?
t2::b
保证是一个独立的内存位置?考虑数据竞争。 - Kerrek SB%zu
打印sizeof
的结果。 - phuclv