C语言中位域排序语义的澄清 [英] Clarification about Bit-field ordering semantics in C

查看:115
本文介绍了C语言中位域排序语义的澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解有关位字段(6.7.2.1:10)的C99草案标准(N1256)段落的确切含义:

I have troubles understanding the exact meaning of a paragraph of C99 draft standard (N1256) about bit-fields (6.7.2.1:10):

6.7.2.1结构和联合说明符

[...]

6.7.2.1 Structure and union specifiers

[...]

语义

[...]

实现可以分配任何足够大的可寻址存储单元来容纳位字段.如果有足够的空间,应将紧随结构中另一个位域之后的位域打包到同一单元的相邻位中.如果剩余空间不足,则将实现不当的位字段放入下一个单元还是与相邻单元重叠. 单位内位域的分配顺序(从高位到低位或从低位到高位)是实现定义的. 未指定可寻址存储单元.

An implementation may allocate any addressable storage unit large enough to hold a bit-field. 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.

强调的句子将我的英语能力扩展到了极限:我不明白它是指一个单元中的各个位域,还是指各个位域或其他域中的位排序.

The emphasized sentence stretches my English skills to the limit: I don't understand if it refers to individual bit-fields inside a unit, or to bits ordering inside the individual bit-fields or something else.

我将通过一个例子来使我的疑问更加清楚.让我们假设unsigned int是16位,该实现选择一个unsigned int作为可寻址存储单元(并且字节为8位宽),并且不会出现其他对齐或填充问题:

I'll try to make my doubt clearer with an example. Let's assume that unsigned ints are 16 bits, that the implementation chooses an unsigned int as the addressable storage unit (and that bytes are 8 bits wide), and no other alignment or padding issues arise:

struct Foo {
    unsigned int x : 8;
    unsigned int y : 8;
};

因此,假设xy字段存储在同一单元内,那么根据该语句定义的实现是什么?据我了解,这意味着x可以存储在未签名的int单元中,或者存储在比y更低的地址上,反之亦然,但是我不确定,因为凭直觉我会认为,如果没有位字段与两个基础存储单元重叠,声明顺序将对基础位字段施加相同的顺序.

thus, assuming x and y fields are stored inside the same unit, what is implementation-defined according to that sentence? As I understand it, it means that inside that unsigned int unit, x can be stored either at a lower address than y or vice-versa, but I'm not sure, since intuitively I'd think that if no bit fields overlaps with two underlying storage units, the declaration order would impose the same ordering for the underlying bit-fields.

注意:我担心我在这里缺少一些术语上的细微之处(或者,更糟糕的是,一些技术上的术语),但是我不明白哪个.

Note: I fear I'm missing some terminology subtlety here (or, worse, some technical one), but I couldn't understand which.

任何指针都表示赞赏.谢谢!

Any pointer appreciated. Thanks!

推荐答案

我真的看不出有什么不清楚的地方

I don't really see what is unclear with

单位内位域的分配顺序(从高到高) 低阶或从低阶到高阶)是实现定义的.

The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined.

它讨论的是位字段的分配,而不是字段内部的位.因此,除了非位域成员外,您无法确定可寻址单元中位域的排序顺序.

It talks about the allocation of a bit-field, and not the bits inside a field. So other than for non-bit-field members, you can't be sure in what order bit-fields inside an addressable unit are ordered.

否则,必须保证位字段本身的表示与基础类型相同",并分为值位和符号位(如果适用).

Otherwise the representation of the bit-field itself is guaranteed to be "the same" as the underlying type, with a division into value bits and a sign bit (if applicable).

从本质上讲,它是指包含位字段的存储单元的结构是实现定义的,并且您不应该尝试通过其他方式(union或类似方式)访问这些位,因为这会使您的代码成为现实.不可携带.

In essence it says that the anatomy of the storage unit that contains the bit-fields is implementation defined, and you shouldn't try to access the bits through other means (union or so) since this would make your code non-portable.

这篇关于C语言中位域排序语义的澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆