结构与联合之间的区别 [英] Difference between a Structure and a Union

查看:55
本文介绍了结构与联合之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有很好的例子来说明 struct union 之间的区别?基本上,我知道 struct 使用其成员的所有内存,而 union 使用最大的成员内存空间.操作系统级别是否有其他差异?

Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?

推荐答案

使用联合,您只能使用其中一个元素,因为它们都存储在同一位置.当您要存储可能是几种类型之一的东西时,这很有用.另一方面,结构的每个元素都有一个单独的存储位置,并且所有这些元素都可以一次使用.

With a union, you're only supposed to use one of the elements, because they're all stored at the same spot. This makes it useful when you want to store something that could be one of several types. A struct, on the other hand, has a separate memory location for each of its elements and they all can be used at once.

要举一个具体的用法示例,我前一段时间正在研究Scheme解释器,实际上是将Scheme数据类型覆盖在C数据类型上.这涉及在结构中存储一个指示值类型的枚举和一个用于存储该值的联合.

To give a concrete example of their use, I was working on a Scheme interpreter a little while ago and I was essentially overlaying the Scheme data types onto the C data types. This involved storing in a struct an enum indicating the type of value and a union to store that value.

union foo {
  int a;   // can't use both a and b at once
  char b;
} foo;

struct bar {
  int a;   // can use both a and b simultaneously
  char b;
} bar;

union foo x;
x.a = 3; // OK
x.b = 'c'; // NO! this affects the value of x.a!

struct bar y;
y.a = 3; // OK
y.b = 'c'; // OK

编辑:如果您想知道将x.b设置为'c'会将x.a的值更改为什么,从技术上讲它是未定义的.在大多数现代机器上,char是1个字节,int是4个字节,因此给x.b赋予值'c'也会给x.a的第一个字节赋予相同的值:

edit: If you're wondering what setting x.b to 'c' changes the value of x.a to, technically speaking it's undefined. On most modern machines a char is 1 byte and an int is 4 bytes, so giving x.b the value 'c' also gives the first byte of x.a that same value:

union foo x;
x.a = 3;
x.b = 'c';
printf("%i, %i\n", x.a, x.b);

打印

99, 99

两个值为什么相同?因为int 3的最后3个字节全为零,所以也将其读取为99.如果我们为x.a输入更大的数字,您会发现情况并非总是如此:

Why are the two values the same? Because the last 3 bytes of the int 3 are all zero, so it's also read as 99. If we put in a larger number for x.a, you'll see that this is not always the case:

union foo x;
x.a = 387439;
x.b = 'c';
printf("%i, %i\n", x.a, x.b);

打印

387427, 99

要仔细查看实际的内存值,让我们以十六进制设置和打印这些值:

To get a closer look at the actual memory values, let's set and print out the values in hex:

union foo x;
x.a = 0xDEADBEEF;
x.b = 0x22;
printf("%x, %x\n", x.a, x.b);

打印

deadbe22, 22

您可以清楚地看到0x22覆盖了0xEF的位置.

You can clearly see where the 0x22 overwrote the 0xEF.

但是

在C语言中,未定义int的字节顺序.该程序在Mac上用0x22覆盖了0xEF,但是在其他平台上,它会覆盖0xDE,因为该顺序组成int的字节中的所有字节都被反转了.因此,在编写程序时,永远不要依赖于覆盖联合中的特定数据的行为,因为它不具有可移植性.

In C, the order of bytes in an int are not defined. This program overwrote the 0xEF with 0x22 on my Mac, but there are other platforms where it would overwrite the 0xDE instead because the order of the bytes that make up the int were reversed. Therefore, when writing a program, you should never rely on the behavior of overwriting specific data in a union because it's not portable.

有关字节顺序的更多信息,请查看 endianness .

For more reading on the ordering of bytes, check out endianness.

这篇关于结构与联合之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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