工会在与C结构 [英] Unions versus structures in C

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

问题描述

这背后的问题的想法是要了解使用联盟和不同的方式使用它,从而节省内存的更深层次的概念..我的所有问题是 -

The idea behind this question is to understand the deeper concepts of using union and using it in different way so as to save memory.. My question to all is--

让我们说有一个结构

struct strt
{
   float f;
   char c;
   int a;
}

和相同的结构重新$ P $工会psented

and the same structure represented in union

union unin
{
   float f;
   char c;
   int a;
}

如果我分配值构造成员一个接一个,然后打印出来,它会
打印。但在工会的它不发生的情况下,一些覆盖正在做..

If I allocate values to structure members one after another and then print them, it gets printed. But in case of union it doesn't happen, some overwriting is being done..

所以,我需要找出哪些可以存储F,C,使用一个联合的值,然后我可以打印相同的方法。 (适用任何操作或任何..),但我在寻找这种技术的..没有人在那里可以指导我或者给我任何想法?

So I need to find out a method which can store the values of f,c,a using union and then I can print the same. (Apply any operations or anything..) but I am in search of this technique.. Can anybody out there guide me or give me any idea?

推荐答案

如果你看一个结构如何存储它的价值,这将是这样的:

If you were to look at how a struct stores its values, it would be something like this:

|0---1---2---3---|4---|5---6---7---8---|
|ffffffffffffffff|    |                | <- f: Where your float is stored
|                |cccc|                | <- c: Where your char is stored
|                |    |aaaaaaaaaaaaaaaa| <- a: Where your int is stored

所以,当你改变f的值,你实际上是在不断变化的字节0-3。当你改变你的字符,当你改变你的INT,你实际上是在不断变化的字节5-8,实际上改变4个字节。

So when you change the value of f, you are actually changing bytes 0-3. When you change your char, you are actually changing byte 4. When you change your int, you are actually changing bytes 5-8.

如果你现在怎么看待工会店它的价值,这将是这样的:

If you now look at how a union stores its values, it would be something like this:

|0---1---2---3---|
|ffffffffffffffff| <- f: where your float is stored
|cccc------------| <- c: where your char is stored
|aaaaaaaaaaaaaaaa| <- a: where your int is stored

所以,现在,当我改变f的价值,我改变字节0-3。这是由于C存储在0字节,当您更改楼还改变C和A!当您更改C,你改变的f和部分 - 当你改变,你改变c和f。这就是你的覆盖正在发生的事情。当你包3的值到一个内存地址,你不是在所有的节省空间;你只是创建3种不同的观察和改变相同的数据的方式。你真的没有一个int,float,并且在工会字符 - 在物理层面,你拿到32位,这可以被看作是一个int,浮点数,或一个char。改变一个人是的意味着的改变别人。如果你不希望他们能改变对方,然后用一个结构。

So now, when I change the value of f, I am changing bytes 0-3. Since c is stored in byte 0, when you change f, you also change c and a! When you change c, you're changing part of f and a - and when you change a, you're changing c and f. That's where your "overwriting" is happening. When you pack the 3 values into the one memory address, you're not "saving space" at all; you're just creating 3 different ways of looking at and changing the same data. You don't really have an int, a float, and a char in that union - at the physical level, you've just got 32 bits, which could be viewed as an int, a float, or a char. Changing one is meant to change the others. If you don't want them to change each other, then use a struct.

这就是为什么GCC告诉你,你的结构是9个字节长,而你的工会只有4 - 这不是节省空间 - 这只是结构和联合是不一样的东西。

This is why gcc tells you that your struct is 9 bytes long, while your union is only 4 - it's not saving space - it's just that structs and unions are not the same thing.

这篇关于工会在与C结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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