c++ - C++结构中的位字段声明

标签 c++ struct bit-fields

我在阅读 C++ 的 ISO 标准时,发现了这个位域声明。下面的代码我不是很清楚

struct {
 char a;
 int b:5,
 c:11,
 :0,
 d:8;
 struct {int ee:8;}
 e;
 } 

这里指定字段a、d、e.ee有不同的内存位置,可以使用多线程独立修改。位域 b 和 c 使用相同的内存位置,因此不能同时修改它们。我不明白为 c 使用两个位字段的意义,即 c:11,:0,。任何人都可以清除我对此的看法吗?谢谢

最佳答案

你问了;

I dont understand the significance of using two bit fields for c i.e, c:11,:0,. can anybody clear my vision about this?

c 不是使用两个位字段定义的。第二个是未命名的位域。宽度为零的未命名位域具有特殊意义。这就是标准关于未命名位域的说法。

A declaration for a bit-field that omits the identifier declares an unnamed bit-field. Unnamed bit-fields are not members and cannot be initialized. [ Note: An unnamed bit-field is useful for padding to conform to externally-imposed layouts. — end note ] As a special case, an unnamed bit-field with a width of zero specifies alignment of the next bit-field at an allocation unit boundary. Only when declaring an unnamed bit-field may the value of the constant-expression be equal to zero.

关于c++ - C++结构中的位字段声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27577206/

相关文章:

c++ - 如何 stub /模拟 Qt 类?

使用枚举类的 C++11 标准符合位掩码

c - 包含用不同类型定义的位域的结构的大小

c++ - 如何处理位字段中的单位( bool )成员?

c++ - 具有无符号短整数的结构的大小

c++ - XP 滚动条在 Windows7/Vista 中乱七八糟

c++ - 在 SELECT 中拆分一个 varbinary

c++ - Android NDK R5 和对 C++ 异常的支持

c++ - 关于 C 与 C++ 中的结构变量声明

c - 在 C 中动态重新分配结构数组