为什么与混合数据类型的位字段相比,相同数据类型的位字段的大小更小

发布于 2024-07-14 17:31:01 字数 309 浏览 4 评论 0原文

我很好奇为什么具有相同数据类型的位字段比混合的位字段占用的大小更小 数据类型。

struct xyz 
{ 
  int x : 1; 
  int y : 1; 
  int z : 1; 
}; 


struct abc 
{ 
  char x : 1; 
  int y : 1; 
  bool z : 1; 
}; 

大小 (xyz) = 4 sizeof(abc) = 12。

我使用的是 VS 2005,64 位 x86 机器。

有点机器/编译器级别的答案会很棒。

I am curious to know why bit fields with same data type takes less size than with mixed
data types.

struct xyz 
{ 
  int x : 1; 
  int y : 1; 
  int z : 1; 
}; 


struct abc 
{ 
  char x : 1; 
  int y : 1; 
  bool z : 1; 
}; 

sizeof(xyz) = 4
sizeof(abc) = 12.

I am using VS 2005, 64bit x86 machine.

A bit machine/compiler level answer would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苯莒 2024-07-21 17:31:01

结盟。

您的编译器将以对您的架构有意义的方式对齐变量。 在您的情况下,charintbool 的大小不同,因此它将根据该信息而不是您的位字段提示。

这个问题关于此事。

解决方案是向编译器提供#pragma指令或__attributes__以指示它忽略对齐优化。

Alignment.

Your compiler is going to align variables in a way that makes sense for your architecture. In your case, char, int, and bool are different sizes, so it will go by that information rather than your bit field hints.

There was some discussion in this question on the matter.

The solution is to give #pragma directives or __attributes__ to your compiler to instruct it to ignore alignment optimizations.

你的心境我的脸 2024-07-21 17:31:01

C 标准(1999 版,§6.7.2.1,第 102 页,第 10 点)是这样说的:

实现可以分配任何足够大的可寻址存储单元来容纳
位域。 如果还有足够的空间,则紧接着另一个位字段
结构中的位字段应打包到同一单元的相邻位中。

似乎没有任何措辞允许打包受到字段类型的影响。 因此我得出的结论是这是一个编译器错误。

无论哪种情况,gcc 在 Linux 下的 32 位和 64 位机器上都会生成一个 4 字节结构。 我没有VS,无法测试。

The C standard (1999 version, §6.7.2.1, page 102, point 10) says this:

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.

There does not seem to be any wording to allow the packing to be affected by the types of the fields. Thus I would conclude that this is a compiler bug.

gcc makes a 4 byte struct in either case, on both a 32-bit and a 64-bit machine, under Linux. I don't have VS and can't test that.

滴情不沾 2024-07-21 17:31:01

这是编译器错误或某些代码错误。
结构中分配的所有位总是尝试定义最高数据类型的大小。
例如,在 struct xyz 中,最高数据类型的大小是 4,即 int。
以类似的方式,第二个结构 abc 的最高数据类型大小为 4(对于 int)。

就好像我们改变结构变量如下:
结构体abc
{
字符a:1;
字符b:1;
布尔 c:1;
};

sizeof(abc) 将是 1 而不是 4。由于 size 最高数据类型是 1 并且所有位都适合 1 个字节的 char。

可以通过更改结构中的数据类型来执行各种测试。

基于旧结构的输出链接:
访问 http://codepad.org/6j5z2CEX

链接获取基于我定义的上述结构的输出:
访问 http://codepad.org/fqF9Ob8W

为了避免 sizeof 结构的此类问题,我们应使用 #pragma 正确打包结构包宏。

It's complier bug or some code error.
All bits assigned in the structure always try to make sizeof highest data type defined.
e.g. In struct xyz sizeof highest data type is 4 i.e. of int.
In the similar fashion for second structure abc highest data type size is 4 for int.

Where as if we change variables of structure as following:
struct abc
{
char a:1;
char b:1;
bool c:1;
};

sizeof(abc) would be 1 not 4. Since size highest data type is 1 and all bits fit into 1byte of char.

various tests could be performed by changing data type in the structure.

Link for output based on old structure:
Visit http://codepad.org/6j5z2CEX

Link for output based on above structure defined by me:
Visit http://codepad.org/fqF9Ob8W

To avoid such problems for sizeof structures we shall properly pack structures using #pragma pack macro.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文