旧行李 2008-12-06 17:56 采纳率: 25%
浏览 344
已采纳

结构与联盟的区别

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?

转载于:https://stackoverflow.com/questions/346536/difference-between-a-structure-and-a-union

  • 写回答

15条回答 默认 最新

  • 斗士狗 2008-12-06 17:59
    关注

    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.

    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
    

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

    prints

    99, 99
    

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

    prints

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

    prints

    deadbe22, 22
    

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

    BUT

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(14条)

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示