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

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

问题描述

有什么好的例子可以说明 structunion 之间的区别吗?基本上我知道 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
", x.a, x.b);

印刷品

99, 99

为什么这两个值相同?因为 int 3 的最后 3 个字节都是 0,所以它也读为 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
", 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
", 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天全站免登陆