难道位域类型影响结构alignement [英] Does the type of bitfield affect structure alignement

查看:100
本文介绍了难道位域类型影响结构alignement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:

 结构bf_struct1
{
    uint64_t中BF1:1;
    uint64_t中BF2:6;
    uint64_t中BF3:2;
    uint64_t中BF4:55;
}结构bf_struct2
{
    uint8_t有BF1:1;
    uint8_t有BF2:6;
    uint8_t有BF3:2;
    uint64_t中BF4:55;
}

请问结构成员对齐取决于类型位域的成员?


解决方案

从的马的嘴


6.7.2.1结构和联合说明符的结果
...结果
5位字段应具有一个类型,是一个合格或不合格的版本 _Bool,符号int ,无符号整型或某些其它实现定义的类型。它是
实现定义原子类型是否允许。结果
...结果
11实现可以分配任何可寻址存储单元,大到足以容纳一个比特。
如果有足够的空间仍然存在,紧跟在另一个位字段位域
结构应包装到同一单元的相邻比特。如果没有足够的空间仍然存在,
不适合的位字段是否被放入下一个单元或重叠相邻单元是
实现定义。位域的分配单位(高阶到内的顺序
低阶或低阶到高阶)是实现定义的。的对准
可寻址存储单元是不确定的。

简短的回答:可以,根据不同的实现。

I have the following structures:

struct bf_struct1
{
    uint64_t bf1 : 1;
    uint64_t bf2 : 6;
    uint64_t bf3 : 2;
    uint64_t bf4 : 55;
}

struct bf_struct2
{
    uint8_t bf1 : 1;
    uint8_t bf2 : 6;
    uint8_t bf3 : 2;
    uint64_t bf4 : 55;
}

Does the structure member alignment depend on type of a bitfield members?

解决方案

From the horse's mouth:

6.7.2.1 Structure and union specifiers
...
5 A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type. It is implementation-defined whether atomic types are permitted.
...
11 An implementation may allocate any addressable storage unit large enough to hold a bitfield. 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.

Short answer: it can, depending on the implementation.

这篇关于难道位域类型影响结构alignement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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