简体   繁体   English

位域和存储单元

[英]bit-field and storage unit

I have a doubt on my compiler using a 32 bit compiler (GCC MinGW) on a 64 bit machine. 我对使用64位计算机上的32位编译器(GCC MinGW)的编译器有疑问。 When I use this struct: 当我使用这个结构时:

struct S 
{
    unsigned int a : 2; 
    unsigned int b : 3;
    unsigned int c : 4;
    _Bool d : 1;
} s;

sizeof s returns 8 so my compiler is using my machine word that's 8 byte (64 bit) for packing bit fields. sizeof s返回8,所以我的编译器使用我的机器字,即8字节(64位)来封装位字段。 Why if the compiler is 32 bit? 为什么编译器是32位? In fact sizeof(int) give me 4. 事实上sizeof(int)给我4。

Moreover if I declare my struct as: 此外,如果我将我的结构声明为:

struct S 
{
    char a; 
    char b;
    char c;
    _Bool d;;
} s;

sizeof s give me 4 so it is better to pack the structure in this way to save more space. sizeof s给我4,所以最好以这种方式包装结构以节省更多空间。 So often is said that using bit field with structure can save space... but I think this is not always so... Am I missing some information? 所以经常说使用带结构的位字段可以节省空间......但我认为这并不总是如此......我是否缺少一些信息?

Bitfields are largely implementation-defined or even unspecified. 位域主要是实现定义的,甚至是未指定的。

My guess is that switching from unsigned int to _Bool made your compiler start a completely new bit-field. 我的猜测是从unsigned int切换到_Bool会使编译器启动一个全新的位字段。

Anyway, you are right with the final resolution: 无论如何,你对最终决议是正确的:
If you want something reliable and/or portable, pack them yourself. 如果您想要可靠和/或便携的东西,请自行包装。

Here all the relevant parts from C99+amendments (n1570). 这里是C99 +修正案(n1570)的所有相关部分。

6.7.2.1 Structure and union specifiers 6.7.2.1结构和联合说明符

[...] [...]
5 A bit-field shall have a type that is a qualified or unqualified version of _Bool , signed int , unsigned int , or some other implementation-defined type. 5位字段的类型应为_Boolsigned intunsigned int或其他实现定义类型的限定或非限定版本。 It is implementation-defined whether atomic types are permitted. 它是实现定义的,是否允许原子类型。
[...] [...]
10 10 A bit-field is interpreted as having a signed or unsigned integer type consisting of the specified number of bits.125) If the value 0 or 1 is stored into a nonzero-width bit-field of type _Bool , the value of the bit-field shall compare equal to the value stored; 10 10位字段被解释为具有由指定位数组成的有符号或无符号整数类型.125)如果值0或1存储在_Bool类型的非零宽度位字段中,则该值为比特字段的比较等于存储的值; a _Bool bit-field has the semantics of a _Bool . _Bool位字段具有_Bool的语义。
11 An implementation may allocate any addressable storage unit large enough to hold a bitfield. 11实现可以分配任何足够大的可寻址存储单元来保存位域。 If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. 如果剩余足够的空间,则紧跟在结构中的另一个位字段之后的位字段将被打包到相同单元的相邻位中。 If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. 如果剩余的空间不足,则是否将不适合的位域放入下一个单元或重叠相邻单元是实现定义的。 The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. 单元内的位域分配顺序(高阶到低阶或低阶到高阶)是实现定义的。 The alignment of the addressable storage unit is unspecified. 未指定可寻址存储单元的对齐。
12 A bit-field declaration with no declarator, but only a colon and a width, indicates an unnamed bit-field.126) As a special case, a bit-field structure member with a width of 0 indicates that no further bit-field is to be packed into the unit in which the previous bitfield, if any, was placed. 12没有声明符但只有冒号和宽度的位字段声明表示未命名的位字段.126)作为一种特殊情况,宽度为0的位字段结构成员表示没有其他位字段将被打包到放置前一个位域(如果有的话)的单元中。
[...] [...]
15 Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. 15在结构对象中,非位字段成员和位字段所在的单元具有按声明顺序增加的地址。 A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. 指向适当转换的结构对象的指针指向其初始成员(或者如果该成员是位字段,则指向它所在的单元),反之亦然。 There may be unnamed padding within a structure object, but not at its beginning. 结构对象中可能存在未命名的填充,但不是在其开头。
[...] [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM