简体   繁体   English

C ++位域结构大小定义(为什么打包得更大?)

[英]C++ Bitfield Struct size definition (Why is it packed larger?)

I have a question about bit packing in C++. 我对C ++中的位打包有疑问。

Lets say we have a struct defined in C++. 可以说我们有一个用C ++定义的结构。 Here it is below: 在下面:

typedef struct
{
    unsigned long byte_half : 4;             //0.5
    unsigned long byte_oneAndHalf : 12;      //2
    union
    {
        unsigned long byte_union_one_1 : 8;  //3
        unsigned long byte_union_one_2 : 8;  //3
        unsigned long byte_union_one_3 : 8;  //3
    };
    unsigned long byte_one        : 8;       //4
}LongStruct;

It is a struct called LongStruct. 这是一个称为LongStruct的结构。 From the looks of it, it occupies 4 bytes, and fits into a long. 从它的外观来看,它占用4个字节,并且很长。 Now I execute the following line: 现在,我执行以下行:

int size = sizeof(LongStruct);

I take a look at size, expecting it to have the value 4. Turns out I get 12 instead. 我看一下大小,期望它的值为4。结果我得到的是12。 In what way am I incorrectly visualizing my struct? 我以什么方式错误地可视化了我的结构?

Thank you in advance for any help you can give me. 预先感谢您能给我的任何帮助。

The anonymous union is not substituting its attributes, but is taking a four-byte chunk in the middle of your bit field struct. 匿名联合并没有替代其属性,而是在您的位字段结构中间占据了一个四字节的块。 So your first two members are two bytes + two padding. 因此,您的前两个成员是两个字节+两个填充。 Then your union is one byte plus three padding. 那么您的并集是一个字节加三个填充。 Then your final member is one byte and three more padding. 然后,您的最终成员是一个字节,再加上三个填充。 The total is the 12 you observe. 总数是您观察到的12。

I'll try to dig into the standard to see exactly what it says about anonymous union bitfields. 我将尝试深入研究该标准,以确切了解其对匿名联合位域的说明。 Alternately if you describe the real problem you're trying to solve we could look into answering that as well. 另外,如果您描述了您要解决的实际问题,我们也可以考虑回答。

As an aside you have this tagged C++ so strongly prefer struct X {}; 顺便说一句,您已将此标记为C ++,因此非常喜欢struct X {}; over typedef struct {} X; 超过typedef struct {} X;

The union is expanded to a long , so its size is 4 bytes instead of 1 byte. union扩展为long ,因此其大小为4个字节,而不是1个字节。

As a result, it is aligned to a 4-byte offset from the beginning of the structure. 结果,它与结构开头的偏移量对齐为4个字节。

In addition, the entire structure is expanded to be a multiple of 4 bytes in size. 另外,整个结构被扩展为4个字节的倍数。

So the actual structure looks like this: 因此,实际结构如下所示:

unsigned long byte_half       :  4; // bits  0 -  3
unsigned long byte_oneAndHalf : 12; // bits  4 - 15
unsigned long byte_padding_1  : 16; // bits 16 - 31 // align union

union
{
    unsigned long byte_union_one_1 :  8; // bits 32 - 39
    unsigned long byte_union_one_2 :  8; // bits 32 - 39
    unsigned long byte_union_one_3 :  8; // bits 32 - 39
    unsigned long byte_padding_2   : 24; // bits 40 - 63 // expand union
};

unsigned long byte_one       :  8; // bits 64 - 71
unsigned long byte_padding_3 : 24; // bits 72 - 95 // expand structure

Hence the total size is 96 bits (12 bytes). 因此,总大小为96位(12个字节)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM