问题
I have looked into this post and explored the use cases of union. Every source is saying that it is memory efficient over structures, I understand this.
But confusion arises when you say:
"Variables of Union share same memory, when you change the value of one variable, then other's value gets changed, and only one variable is accessible at a time"
So why is there a need to declare union and its child variables, why can not I use 1 simple variable by allocating the biggest memory as union biggest variable?
Consider this example:
union {
int length;
int breadth;
char status;
} dimension;
Versus
int dimension; # Now this can store the highest value similar to length and breadth.
回答1:
So why is there a need to declare union and its child variables, why can not I use 1 simple variable by allocating the biggest memory as union biggest variable?
You could do that, but a union makes it much easier.
Your particular example does not make much sense, mostly because you have two of the same type, but also because all are integers. So let's take an example from the post you linked:
union {
int i;
float f;
} u;
This means that you can use u.i
when you want to treat the memory as int
or u.f
if you want to treat it like a floatfloat
.
So let's say you want to solve this without a union and just declare a variable "big enough". Which type do you pick? int
is at least 16 bit, and float
is at least 32 bit. So we pick a float
then? Nope, because it might be the case that on the target system, an int
is 64 bit and a float
32. So let's overdo things and pick the largest type that exists? Well, you could, but that kind of defeats the purpose of saving memory.
And how do we access them as different types if declared as variables? Consider this code:
float x;
int y = *(int*) x;
Should work nice, right? Nope, apart from the problem that sizes may vary, you will also run into problems with representations. There are a number of different ways of representing both integers and floats. You may also encounter problems with endianess.
You can mimic the behavior of unions without actually using them, but it will require A LOT of extra work. The resulting code is very likely to contain a lot of bugs, and is probably much slower and less portable too.
One use case is to achieve polymorphism. Here is a very simple example, and tbh, it does not look it make things much easier, but that's commonly the case with examples. Suppose we have this:
void print_float(float f)
{
printf("Value: %f\n", f);
}
void print_int(int i)
{
printf("Value: %d\n", i);
}
That could be replaced by this:
struct multitype {
union {
int i;
float f;
} data;
enum { INT, FLOAT } type;
};
void print(struct multitype x)
{
switch(x.type) {
case INT: printf("Value: %d\n", x.data.i); break;
case FLOAT: printf("Value: %f\n", x.data.f); break;
}
}
回答2:
I think we use union when you are not clear about the type of the variable.
I used union when I convert assemble language to c code. Sometimes one variable used as an int variable and sometimes used as a pointer. At this time I used union.
union {
int data;
void* handle;
}
I hope it's helpful for your understanding.
来源:https://stackoverflow.com/questions/62975043/how-union-is-helpful-over-a-variable