Yes, they can be. Why not? Bit-fields in unions behave in the same way they behave anywhere else. There's nothing special about bit-fields in unions (or unions with bit-fields).
Yes it is possible, but I would recommend against it. The length and packing of bitfields is not portable. The size of the union will be difficult to predict (see here). There is a certain amount of complexity that you introduce into the code when you use unions or bitfields. While this complexity may be acceptable in your code, combining the two may result in an unacceptable amount of complexity. If you are using unions, structs and bitfields, you run into problems with memory alignment.
If this is throwaway code that only needs to be built and run on one machine, then it's probably fine. However, if you are checking this into version control where it will live on forever, I recommend against it.
If you give an example of why you want to do this, I or someone else can suggest a better alternative.
EDIT: clarified based on comments and to ask for feedback.
#include <stdio.h>
union test {
int a:5;
int b:12;
float c;
double d;
int x;
};
int main()
{
union test x;
printf("%d\n", sizeof(x));
x.a = 31;
printf("%d\n", x.a);
printf("%d\n", x.b);
x.c = 1.23;
printf("%d\n", x.a);
printf("%f\n", x.c);
x.x = 31;
printf("%d\n", x.x);
printf("%d\n", x.a);
printf("%d\n", x.b);
}
If you think about how union works, you have the answer, which is yes, of course (why not)? As we expect, the union is big enough to hold the largest datum, and so automatically the smaller. Bitfields are packed into "containers" and the compiler must be able to evaluate their final real size. The following shows some interesting facts (and of course is a wrong usage of a union, but not for the bitfield presence!)
#include <stdio.h>
union test {
int a:5;
int b:12;
float c;
double d;
int x;
};
int main()
{
union test x;
printf("%d\n", sizeof(x));
x.a = 31;
printf("%d\n", x.a);
printf("%d\n", x.b);
x.c = 1.23;
printf("%d\n", x.a);
printf("%f\n", x.c);
x.x = 31;
printf("%d\n", x.x);
printf("%d\n", x.a);
printf("%d\n", x.b);
}
It's only unsafe if you write to one union element and read from a different one. If the details of your implementation ensure that does not happen, then a union containing a bitfied (and presumably other members) has well defined, safe behavior.
发布评论
评论(4)
是的,可以。为什么不呢?联合中的位字段的行为方式与其他地方的行为方式相同。联合中的位字段(或具有位字段的联合)没有什么特别的。
Yes, they can be. Why not? Bit-fields in unions behave in the same way they behave anywhere else. There's nothing special about bit-fields in unions (or unions with bit-fields).
是的,这是可能的,但我建议不要这样做。位域的长度和打包是不可移植的。联合的大小很难预测(请参阅此处)。使用联合或位域时会给代码带来一定程度的复杂性。虽然这种复杂性在您的代码中可能是可以接受的,但将两者结合起来可能会导致不可接受的复杂性。如果您使用联合、结构和位域,则会遇到内存对齐问题。
如果这是只需要在一台机器上构建和运行的一次性代码,那么它可能没问题。但是,如果您将其检查到版本控制中,它将永远存在,我建议您不要这样做。
如果您举例说明为什么要这样做,我或其他人可以提出更好的选择。
编辑:根据评论进行澄清并寻求反馈。
Yes it is possible, but I would recommend against it. The length and packing of bitfields is not portable. The size of the union will be difficult to predict (see here). There is a certain amount of complexity that you introduce into the code when you use unions or bitfields. While this complexity may be acceptable in your code, combining the two may result in an unacceptable amount of complexity. If you are using unions, structs and bitfields, you run into problems with memory alignment.
If this is throwaway code that only needs to be built and run on one machine, then it's probably fine. However, if you are checking this into version control where it will live on forever, I recommend against it.
If you give an example of why you want to do this, I or someone else can suggest a better alternative.
EDIT: clarified based on comments and to ask for feedback.
如果您考虑一下工会是如何运作的,您就会得到答案,当然是肯定的(为什么不呢)?正如我们所期望的,并集足够大以容纳最大的数据,因此自动较小。位域被打包到“容器”中,编译器必须能够评估它们的最终实际大小。下面显示了一些有趣的事实(当然是联合的错误用法,但不适用于位域的存在!)
If you think about how union works, you have the answer, which is yes, of course (why not)? As we expect, the union is big enough to hold the largest datum, and so automatically the smaller. Bitfields are packed into "containers" and the compiler must be able to evaluate their final real size. The following shows some interesting facts (and of course is a wrong usage of a union, but not for the bitfield presence!)
只有当您写入一个联合元素并从另一个联合元素读取时,这才是不安全的。如果您的实现细节确保不会发生这种情况,那么包含 bitfied(可能还有其他成员)的联合具有定义明确的安全行为。
It's only unsafe if you write to one union element and read from a different one. If the details of your implementation ensure that does not happen, then a union containing a bitfied (and presumably other members) has well defined, safe behavior.