c++ - 重新排序位字段神秘地改变了结构的大小

标签 c++ bit-fields structure-packing

出于某种原因,我有一个结构需要跟踪 56 位信息,这些信息被排序为 4 包 12 位和 2 包 4 位。这总共有 7 个字节的信息。

我试过这样的位域

struct foo {
    uint16_t R : 12;
    uint16_t G : 12;
    uint16_t B : 12;
    uint16_t A : 12;
    uint8_t  X : 4;
    uint8_t  Y : 4;
};

并且惊讶地看到 sizeof(foo) 在我的机器(一个 linux x86_64 机器)上用 g++ 版本 12.1 评估为 10。我试过像这样重新排序字段

struct foo2 {
    uint8_t  X : 4;
    uint16_t R : 12;
    uint16_t G : 12;
    uint16_t B : 12;
    uint16_t A : 12;
    uint8_t  Y : 4;
};

令我惊讶的是现在的大小是 8 个字节,这正是我最初的预期。它与我期望第一个解决方案有效生成的结构大小相同:

struct baseline {
    uint16_t first;
    uint16_t second;
    uint16_t third;
    uint8_t  single;
};

我知道大小和对齐以及结构包装,但我真的很困惑为什么第一次排序会增加 2 个额外的字节。没有理由添加超过一个字节的填充,因为我请求的 56 位可以恰好包含 7 个字节。

最小工作示例 Try it on Wandbox

我错过了什么?

PS:如果我们将 uint8_t 更改为 uint16_t,这些都不会改变

最佳答案

如果我们创建一个 struct foo 的实例,将其置零,设置字段中的所有位,并打印字节,并对每个字段执行此操作,我们将看到以下内容:

R: ff 0f 00 00 00 00 00 00 00 00 
G: 00 00 ff 0f 00 00 00 00 00 00 
B: 00 00 00 00 ff 0f 00 00 00 00 
A: 00 00 00 00 00 00 ff 0f 00 00 
X: 00 00 00 00 00 00 00 f0 00 00 
Y: 00 00 00 00 00 00 00 00 0f 00 

所以似乎正在发生的事情是每个 12 位字段都在一个新的 16 位存储单元中开始。然后第一个 4 位字段填充前面 16 位单元中的剩余位,然后最后一个字段占用最后一个单元中的 4 位。这占用 9 个字节并且由于最大的字段(在本例中为位字段存储单元)为 2 个字节宽,因此在末尾添加一个字节的填充。

因此看起来 12 位字段(具有 16 位基本类型)保存在单个 16 位存储单元中,而不是在多个存储单元之间拆分。

如果我们对修改后的结构做同样的事情:

X: 0f 00 00 00 00 00 00 00 
R: f0 ff 00 00 00 00 00 00 
G: 00 00 ff 0f 00 00 00 00 
B: 00 00 00 00 ff 0f 00 00 
A: 00 00 00 00 00 00 ff 0f 
Y: 00 00 00 00 00 00 00 f0 

我们看到X占据了前16位存储单元的4位,然后R占据了剩下的12位。其余字段如前所述填写。这会导致使用 8 个字节,因此不需要额外的填充。

虽然位域排序的具体细节是实现定义的,C standard确实设定了一些规则。

来自第 6.7.2.1p11 节:

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.

和 6.7.2.1p15:

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared.

关于c++ - 重新排序位字段神秘地改变了结构的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73212841/

相关文章:

c++ - 强制 CMake 在每次构建时生成 configure_file 目标

c++ - 在 C++ 11 中使用来自 lambda 的值

c - 如何初始化在 typdef union 中深埋 2 层的 int

postgresql - postgresql 中 varbinary(10) 的等价物是什么

c++ - 将结构体成员声明为 uint32_t 时的额外字节

c++ - 如何在具有模板函数的类中使用类型安全的 union (变体)?

c++ - 使用 0xFFFFFFFF 是一种可靠的方法来设置 32 位类型的所有位吗?

c - C中结构的大小

opengl - 1 字节对齐与 4 字节对齐(像素封装)

python - 如何使用 C++ 中的引用实现类似 python 的行为?