GCC实现位域的一个错误 [英] A bug in GCC implementation of bit-fields

查看:311
本文介绍了GCC实现位域的一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C11中,使用以下结构:

  struct S {
unsigned a:4;
_Bool b:1;
};

GCC将其作为 unsigned (4字节),其中使用了4位,后面是使用1位的 _Bool (4字节),总大小为8字节。



请注意,C99和C11特别允许 _Bool 作为位域成员。 C11标准(也可能是C99)也在第6.7.2.1节结构和联合说明符中声明:


可以分配足够大的任何可寻址存储单元来容纳比特字段。如果有足够的空间,紧接在结构中的另一个位字段之后的位字段将被打包到相同单元的相邻位中。




所以我相信上面的成员 b 应该已经打包到为成员 a 分配的存储单元中了。 ,产生一个总大小为4字节的结构。



当两个成员使用相同类型时,或者当 unsigned 和其他签名,但类型无符号和<$ c

有人可以证实我对标准的解释,而且这确实是一个GCC错误?我还对解决方法感兴趣(一些编译器开关,编译指示, __属性__

code> ...)。



我使用gcc 4.7.0和 -std = c11 (虽然其他settin gs显示相同的行为。)

解决方案

描述的行为与C99和C11标准不兼容,但是提供了二进制兼容性使用MSVC编译器(它具有不寻常的结构打包行为)。

幸运的是,它可以在 __ attribute __((gcc_struct) )应用于该结构,或通过命令行开关 -mno-ms-bitfields (请参阅 documentation )。


Working in C11, the following struct:

struct S {
  unsigned a : 4;
  _Bool    b : 1;
};

Gets layed out by GCC as an unsigned (4 bytes) of which 4 bits are used, followed by a _Bool (4 bytes) of which 1 bit is used, for a total size of 8 bytes.

Note that C99 and C11 specifically permit _Bool as a bit-field member. The C11 standard (and probably C99 too) also states under §6.7.2.1 'Structure and union specifiers' ¶11 that:

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.

So I believe that the member b above should have been packed into the storage unit allocated for the member a, resulting in a struct of total size 4 bytes.

GCC behaves correctly and packing does occur when using the same types for the two members, or when one is unsigned and the other signed, but the types unsigned and _Bool seem to be considered too distinct by GCC for it to handle them correctly.

Can someone confirm my interpretation of the standard, and that this is indeed a GCC bug?

I'm also interested in a work-around (some compiler switch, pragma, __attribute__...).

I'm using gcc 4.7.0 with -std=c11 (although other settings show the same behavior.)

解决方案

The described behavior is incompatible with the C99 and C11 standards, but is provided for binary compatibility with the MSVC compiler (which has unusual struct packing behavior.)

Fortunately, it can be disabled either in the code with __attribute__((gcc_struct)) applied to the struct, or with the command-line switch -mno-ms-bitfields (see the documentation).

这篇关于GCC实现位域的一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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