这很简单。
struct abc
{
char arr[7]; // occupies 7 bytes
char arr1[2]; // occupies 2 bytes
int i:24; // occupies 3 bytes
};
现在,在(的i
)的第三个声明中,只需要 3 个字节。您已经拥有如下:
0 1 2 3 // All 4 bytes used for `char arr[7]`
0 1 2 3 // 3 more used for `char arr[7]`, 1 used for `char arr1[2]`
0 1 2 3 // 1 used for `char arr1[2]`, and the remaining 3 bytes will be used for `int i:24`
但是如果你使用int i
(无位域),它会消耗 16 个字节,因为
0 1 2 3 // All 4 bytes used for `char arr[7]`
0 1 2 3 // 3 more used for `char arr[7]`, 1 used for `char arr1[2]`
0 1 2 3 // 1 used for `char arr1[2]`, there are still 3 bytes but we need 4 bytes for an `int`
0 1 2 3 // So the compiler will allocate a new 4 byte chunk for `int i`
我想现在已经很清楚了。