结构体声明中的冒号是什么意思,例如:1、:7、:16 或:32?
下面的C++代码是什么意思?
unsigned char a : 1;
unsigned char b : 7;
我猜它创建了两个字符 a 和 b,它们都应该是一个字节长,但我不知道“:1”和“:7”部分的作用。
What does the following C++ code mean?
unsigned char a : 1;
unsigned char b : 7;
I guess it creates two char a and b, and both of them should be one byte long, but I have no idea what the ": 1" and ": 7" part does.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1 和 7 是限制值范围的位大小。它们通常出现在结构和工会中。例如,在某些系统上(取决于
char
宽度和打包规则等),代码:创建一个8位值,一位用于
a
,7位用于b
。通常在 C 中用于访问“压缩”值,例如可能包含在 8 位字符的上半部分中的 4 位 nybble:
对于我们当中的语言律师,C++11 标准的 9.6 部分解释了详细解释一下:
位域 [class.bit]
形式的成员声明符
标识符 opt 属性说明符opt : 常量表达式
指定位域;它的长度由冒号与位字段名称分开。可选的属性说明符属于所声明的实体。位域属性不是类成员类型的一部分。
常量表达式应为值大于或等于零的整型常量表达式。整型常量表达式的值可能大于位域类型的对象表示中的位数;在这种情况下,额外的位用作填充位,并且不参与位字段的值表示。
类对象内位域的分配是实现定义的。位字段的对齐是实现定义的。位字段被打包到一些可寻址的分配单元中。
注意:位域在某些机器上跨接分配单元,而在其他机器上则不然。位字段在某些机器上从右到左分配,而在其他机器上从左到右分配。 - 尾注
The 1 and the 7 are bit sizes to limit the range of the values. They're typically found in structures and unions. For example, on some systems (depends on
char
width and packing rules, etc), the code:creates an 8-bit value, one bit for
a
and 7 bits forb
.Typically used in C to access "compressed" values such as a 4-bit nybble which might be contained in the top half of an 8-bit char:
For the language lawyers amongst us, the 9.6 section of the C++11 standard explains this in detail, slightly paraphrased:
Bit-fields [class.bit]
A member-declarator of the form
identifieropt attribute-specifieropt : constant-expression
specifies a bit-field; its length is set off from the bit-field name by a colon. The optional attribute-specifier appertains to the entity being declared. The bit-field attribute is not part of the type of the class member.
The constant-expression shall be an integral constant expression with a value greater than or equal to zero. The value of the integral constant expression may be larger than the number of bits in the object representation of the bit-field’s type; in such cases the extra bits are used as padding bits and do not participate in the value representation of the bit-field.
Allocation of bit-fields within a class object is implementation-defined. Alignment of bit-fields is implementation-defined. Bit-fields are packed into some addressable allocation unit.
Note: bit-fields straddle allocation units on some machines and not on others. Bit-fields are assigned right-to-left on some machines, left-to-right on others. - end note
我相信那些将是位字段。
I believe those would be bitfields.
严格来说,位域必须是 int、unsigned int 或 _Bool。尽管大多数编译器都会采用任何整型。
参考 C11 6.7.2.1:
您的编译器可能会分配 1 个字节的存储空间,但可以自由地获取更多字节的存储空间。
参考 C11 6.7.2.1:
当您有多个相继声明的位域时,就会节省开支。在这种情况下,如果可能的话,分配的存储将被打包。
参考 C11 6.7.2.1:
Strictly speaking, a bitfield must be a int, unsigned int, or _Bool. Although most compilers will take any integral type.
Ref C11 6.7.2.1:
Your compiler will probably allocate 1 byte of storage, but it is free to grab more.
Ref C11 6.7.2.1:
The savings comes when you have multiple bitfields that are declared one after another. In this case, the storage allocated will be packed if possible.
Ref C11 6.7.2.1: