- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当有填充位时,我有两个关于位字段的问题。
假设我有一个定义为的结构
struct T {
unsigned int x: 1;
unsigned int y: 1;
};
结构体 T 仅实际使用了两位。
问题 1:这两位是否始终是底层 unsigned int 的最低有效位?或者它依赖于平台?
问题2:那些未使用的30位总是初始化为0吗? C 标准对此有何规定?
最佳答案
Question 1: are these two bits always the least significant bits of the underlying unsigned int? Or it is platform dependent?
不,它取决于系统和编译器。您永远无法假设或知道它们是 MSB 或 LSB。
Question 2: Are those unused 30 bits always initialized to 0? What do the C and C++ standards say about it?
取决于您如何初始化结构。未初始化的本地范围的结构可能在填充位/字节中包含垃圾值。使用至少一个初始值设定项集初始化的结构,即使在填充字节中也保证包含零:my_struct = { some };
。
来源
语言律师详细说明为什么上述作品有些复杂。
C17 6.7.9/9(强调我的)是这样说的:
Except where explicitly stated otherwise, for the purposes of this subclause unnamed members of objects of structure and union type do not participate in initialization. Unnamed members of structure objects have indeterminate value even after initialization.
这意味着我们根本不能信任填充位/字节。但上述规则有一个异常(exception)(§20 强调我的):
If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
这意味着如果至少有一个初始值设定项,则适用以下静态存储初始化规则:
C17 6.7.9/10(强调我的):
If an object that has static or thread storage duration is not initialized explicitly, then: /--/
- if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
关于c - 位字段如何与 C 中的位填充相互作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56216243/
我打算在 AngularJS 中尝试一个项目,其中所有组件都将使用 Polymer 开发。现在在开始这个项目之前,我有几个疑问: 在使用 Angular 服务获取数据后,我可以更新 Polymer 组
假设我有多个线程正在从文件中读取,并且我想确保在任何时间点只有一个线程正在从文件中读取。 实现这一点的一种方法是使用 mvar :: MVar ()并确保如下互斥: thread = do ..
对于只有因子列的给定数据框,我想列出数据中没有出现的最多 m 个属性的所有因子组合。下面是一个简单的例子: d cc unlist(cc, recursive=F) -> cc length(cc)
对于相关图,我希望有一个光学正方形图(x 和 y 的像素长度相同),但 x 和 y 也有一定的轴限制。我可以分别获取这两个,但不能同时获取: import matplotlib.pyplot as p
对于相关图,我希望有一个光学正方形图(x 和 y 的像素长度相同),但 x 和 y 也有一定的轴限制。我可以分别获取这两个,但不能同时获取: import matplotlib.pyplot as p
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
我是一名优秀的程序员,十分优秀!