gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:28:31 25 4
gpt4 key购买 nike

我在阅读 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com