union 对变量有何帮助? [英] How union is helpful over a variable?

查看:28
本文介绍了union 对变量有何帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了这篇帖子并探索了 union 的用例.每个消息来源都说它比结构更高效,我理解这一点.

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.

但是当你说:

Union 的变量共享同一个内存,当你改变一个变量的值时,其他的值也会改变,一次只能访问一个变量"

"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"

那么为什么需要声明union及其子变量,为什么我不能通过分配最大内存作为union最大变量来使用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?

考虑这个例子:

union {
   int length;
   int breadth;
   char status;
} dimension;

Versus

int dimension;  # Now this can store the highest value similar to length and breadth.

推荐答案

那么为什么需要声明union及其子变量,为什么我不能通过分配最大内存作为union最大变量来使用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;

这意味着你可以使用 ui 当你想把内存当作 intuf 如果你想把它当作一个floatfloat.

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.

所以假设你想在没有联合的情况下解决这个问题,并且只声明一个足够大"的变量.你选择哪种类型?int 至少为 16 位,float 至少为 32 位.所以我们选择一个 float 那么呢?不,因为在目标系统上可能是这样,int 是 64 位,而 float 是 32 位.所以让我们做些事情并选择存在的最大类型?嗯,你可以,但那样会破坏节省内存的目的.

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.

一个用例是实现多态.这是一个非常简单的示例,而且 tbh,它看起来并没有让事情变得容易得多,但这通常是示例的情况.假设我们有这个:

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);
}

可以用这个代替:

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;
    }
}

这篇关于union 对变量有何帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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