【发布时间】:2012-04-22 19:22:01
【问题描述】:
我想找一个不应该写的理由
struct bitfield {
signed foo:4;
unsigned bar:2;
};
而不是详细地指定
struct bitfield {
signed int foo:4;
unsigned int bar:2;
};
由于位域的每个成员的大小都在冒号后明确指定,是否有任何缺点?
如果我使用char、short、long、long long,这有关系吗?指定的位域位数必须可能总是小于声明类型的宽度吗?
找到一些相关问题:
- Bit-fields of type other than int?
- What is the use of declaring different datatypes inside bitfields?
答案范围从
- 不要使用除(有符号/无符号)
int或_Bool之外的任何其他类型和 -
_Bool、signed int、unsigned int或其他一些实现定义的类型。 (C99 6.2.7.1 (4) )
在这种情况下:这种非特定的其他一些实现定义的类型可能是什么样的,我在这个地方的选择可能会产生什么其他缺点?
【问题讨论】:
-
很遗憾,我不是回答这个问题的合适人选,但这个问题让我很好奇,我浏览了一下网页。 C90、C99 和 C++ 对允许的类型都有不同的定义,更糟糕的是,似乎有些编译器使用类型来确定结构的最小大小(即 long long foo:1 可能导致更大的结构然后是 int foo:1) bytes.com/topic/c/answers/… 有关于该主题的有趣信息
标签: c integer bit-fields