一个结构及在C联盟之间的区别 [英] Difference between a Structure and a Union in C

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

问题描述

有没有什么很好的例子给之间的差值结构联盟
基本上我知道结构使用它的成员和联盟使用最大的成员的内存空间中的所有内存。是否有任何其他操作系统级别的区别?

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.

要给他们使用一个具体的例子,我在做一个计划间preTER一会儿前,我基本上是重叠的计划数据类型到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到价值,从技术上讲这是不确定的。在大多数现代机器炭是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

为什么这两个值相同?因为最后3个字节INT 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.

BUT

在C,字节在一个int顺序为没有定义。:该项目覆盖了0xEF与0x22我的Mac上,但也有其他平台哪里会覆盖写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.

有关字节顺序多读书,请字节顺序

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

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

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