位域声明中的整数宽度是否相关? [英] Is the integer width relevant in bitfield declaration?

查看:139
本文介绍了位域声明中的整数宽度是否相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到我不应该写的原因

I was trying to find a reason why I should not write

struct bitfield {
  signed foo:4;
  unsigned bar:2;
};

而不是详细说明

struct bitfield {
  signed int foo:4;   
  unsigned int bar:2; 
};

由于在冒号后明确指定了位域的每个成员的大小,是否有任何缺点?

As the size of each member of the bitfield is explicitly specified after the colon, could there be any drawbacks?

我使用 char 是否重要,多长?指定位域位的数量是否必须始终小于声明类型的宽度?

Does it matter if I use char, short, long, long long? Must the number of specified bitfield bits probably always be smaller than the width of the declaration type?

发现一些相关问题:

  • Bit-fields of type other than int?
  • What is the use of declaring different datatypes inside bitfields?

答案范围从


  • 不要使用除(签名/未签名) int _Bool
  • $ b $之外的任何其他类型b
  • _Bool signed int unsigned int ,或其他一些实现定义的类型。 (C99 6.2.7.1(4))

  • don't use any other type than (signed/unsigned) int or _Bool and
  • _Bool, signed int, unsigned int, or some other implementation-defined type. (C99 6.2.7.1 (4) )

在此上下文中:这个非特定的某些其他实现定义的类型是的,以及我在这个地方的选择可能会产生什么其他缺点?

In this context: what may this unspecific some other implementation-defined type be like, and what other drawbacks may arise from my choice in this place?

推荐答案

有时和是



C99要求宽度表达式不超过指定类型的对象中的位数所以如果使用小型,代码将无法编译或至少不可移植。见§6.7.2.1(3)。

"Sometimes", and "Yes"

C99 requires that the width expression "not exceed the number of bits in an object of the type that is specified" so if too small a type is used, the code will either not compile or at least not be portable. See §6.7.2.1 (3).

关于更新的第三个问题和一般情况,究竟是什么后果?问题,可能受影响的事情是:可移植性,对齐和填充。该标准仅针对第一个提供了明确的规范。在没有位域的情况下,通常可能基于预测编译器将如何生成最佳对齐值来排列对齐和填充。虽然不能保证,但似乎在某些使用之类的环境中会因为减少对齐和填充而节省内存。

Regarding the updated third question and the general, "exactly what are the consequences?" issue, the things that could be affected are: portability, alignment, and padding. The standard gives clear specifications only for the first. Without bitfields, it's usually possibly to arrange alignment and padding based on predicting what the compiler would do to generate optimally aligned values. Although it is not guaranteed, it seems like in some environments using something like short will save memory due to the reduced alignment and padding that result.

实现精确布局和可移植性偶尔冲突目标的一种可能方法是声明内存中没有位字段的数据结构,可能使用< stdint.h> 类型。然后,如果要使用位字段来解码某些内容,请将内存中的源对象分配给临时变量,该临时变量是位字段和位特定类型的并集,或者通过强制转换来故意违反类型惩罚规则指针。 (Linux一直到处都这样做。)

One possible approach to achieving the occasionally conflicting goals of exact layout and portability is to declare the in-memory data structures without bit fields, perhaps using <stdint.h> types. Then, if you want to use a bit field to decode something, assign the in-memory source object to a temporary variable that's a union of a bit field and a bit-specific type, or, deliberately violate the type punning rules by casting a pointer. (Linux does this all over the place.)

更好的办法可能是避免位字段。

A better way is probably to just avoid bit fields.

这篇关于位域声明中的整数宽度是否相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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