在C中有没有可能让下面的结构体的大小为2?
#include <stdio.h>
struct union_struct {
char foo;
char bar : 2;
union {
char foobar1 : 6;
char foobar2 : 6;
};
};
int main(void)
{
printf("size of union struct: %d\n", sizeof(struct union_struct));
return 0;
}
输出,用gcc编译:
union 结构的大小:3
最佳答案
如果您依赖于实现定义的行为,那么是的,但您必须以不同的方式组织它:
#ifdef UNNAMED_BITFIELDS_ARE_WELL_DEFINED
#define ANON
#else
#define ANON3(X) anonymous__## X ##__
#define ANON2(X) ANON3(X)
#define ANON ANON2(__LINE__)
#endif
struct union_struct {
char foo;
union {
struct {
char bar : 2;
char ANON : 6;
};
struct {
char ANON : 2;
char foobar1 : 6;
};
struct {
char ANON : 2;
char foobar2 : 6;
};
};
};
第一个字节是foo
,第二个字节是匿名 union 。然后匿名 union 有 3 个单字节匿名结构。每个结构(可选)使用未命名的位字段以允许 foobar1
和 foobar2
表示 bar
之后的相同 6 位。
根据我对 C.11 标准的理解,当 UNNAMED_BITFIELDS_ARE_WELL_DEFINED
被定义时,上面的代码是正确的。但是,关于未命名的位域是否具有明确定义的语义似乎存在争议(请参阅下面的评论)。如果未命名的位域没有明确定义的语义,则上面的代码可以将每个 ANON
宏扩展为位域的名称。
但是,C.11 标准仅在 _Bool
、int
和 unsigned
上定义了位域,而使用任何其他位字段的类型是实现定义的(C.11 §6.7.2.1 ¶5)。
关于c - C 中小于 8 位的位字段并集的打包结构的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18647383/