正确使用工会。 [英] properly using unions.

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

问题描述

您好,有人可以指导我使用工会。关于K& K的工会,我读了

R但我发现很难适用于我手边的问题。我想通过使用工会来节省一些空间。我的

问题是:


1.使用工会是否危险?如果我想要

来节省内存,这是值得的吗?它们是否容易出错?


2.我读到在工会的任何

瞬间都不可能访问超过1个成员。这究竟意味着什么?我不能在一个声明中同时使用两个

成员?

Hello, can some one please guide me a little into using unions. I read
about unions in K & R but I am finding it difficult to apply to my
problem at hand. I want to save up some space by using unions . My
questions are :

1. Is it dangerous to use unions ? Is it worth the trouble if I want
to save memory ? Are they error prone ?

2. I read that it is not possible to access more than 1 member at any
instant from a union. What does this actually mean ? I cannot use two
members simultaneously in a single statement ?

推荐答案

pereges说:
pereges said:

您好,有人可以指导我使用工会。关于K& K的工会,我读了

R但我发现很难适用于我手边的问题。我想通过使用工会来节省一些空间。我的

问题是:


1.使用工会是否危险?
Hello, can some one please guide me a little into using unions. I read
about unions in K & R but I am finding it difficult to apply to my
problem at hand. I want to save up some space by using unions . My
questions are :

1. Is it dangerous to use unions ?



不,如果你知道你在做什么就不行。

No, not if you know what you''re doing.


值得麻烦吗如果我想节省内存?
Is it worth the trouble if I want to save memory ?



可能不会,除非你真的真的非常缺乏内存。

Probably not, unless you''re really really really memory-starved.


他们是错误的倾向?
Are they error prone ?



不,但程序员是。 :-)

No, but programmers are. :-)


2.我读到在工会的任何

瞬间无法访问超过1名成员。这究竟意味着什么?我不能在一个声明中同时使用两个

成员?
2. I read that it is not possible to access more than 1 member at any
instant from a union. What does this actually mean ? I cannot use two
members simultaneously in a single statement ?



假设您使用联合来存储一个9字节的对象和一个11美元的b
。联盟可能会有11个字节宽 - 即宽

足以存储你正在放入的最大的东西 - 但它可能是一个稍微宽一点的b / b
对齐原因,例如十二(4的倍数)或

甚至十六(16的倍数)字节。但是11 + 9是20,而且它很可能不会那么大。在任何情况下,你只能合法地访问

前十一个字节。


现在,11个字节足够大,可以存储一个9字节的对象,甚至是
一个11字节的对象。但是存储两者都不够大。


所以在任何一点上,该联盟只能存储一个值或另一个值,

对?它不能同时存储。那么怎样才能尝试同时使用两个值?
?没有两个值用!那里

只有一个值。可能是这一个,也可能是那一个,取决于你最后推到什么的b $ b,但它不可能两者兼而有。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Let''s say you''re using a union to store a nine-byte object and an
eleven-byter. The union will probably be eleven bytes wide - i.e. wide
enough to store the biggest thing you''re putting in it - but it might be a
little bit wider for alignment reasons, e.g. twelve (multiple of 4) or
even sixteen (multiple of 16) bytes. But 11+9 is 20, and it''s pretty
unlikely to be that big. In any case, you can only legitimately access the
first eleven bytes.

Now, eleven bytes is plenty big enough to store a nine-byte object, or even
an eleven-byte object. But it isn''t big enough to store both.

So at any one point, that union can only be storing one value or the other,
right? It can''t store both. So how could it possibly make sense to try to
use both values at the same time? There is no "both values" to use! There
is only one value. It might be this one or it might be that one, depending
on what you shoved in there last, but it can''t possibly be both.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


6月25日上午11点34分,pereges< Brol ... @ gmail.comwrote:
On Jun 25, 11:34 am, pereges <Brol...@gmail.comwrote:

您好,有人可以指导我使用工会。关于K& K的工会,我读了

R但我发现很难适用于我手边的问题。我想通过使用工会来节省一些空间。
Hello, can some one please guide me a little into using unions. I read
about unions in K & R but I am finding it difficult to apply to my
problem at hand. I want to save up some space by using unions .



工会通常不是为了节省空间。它们用在一个

的情况下你不会使用所有成员但在某些条件下访问它们




我的

Unions aren''t generally about saving space. They are used in a
situation in which you don''t use all the members but access them based
on some condition.

My


问题是:


1.使用工会是否危险?如果我想要

来节省内存,这是值得的吗?它们容易出错吗?
questions are :

1. Is it dangerous to use unions ? Is it worth the trouble if I want
to save memory ? Are they error prone ?



如果它是关于节省内存的话,我担心会遇到很多麻烦

而且它不值得它。

If it''s about saving memory, I am afraid its going to a lot of trouble
and it''s not worth it.


2.我读到,在任何

瞬间,不可能访问超过1个成员联盟。这究竟意味着什么?我不能在一个声明中同时使用两个

成员?
2. I read that it is not possible to access more than 1 member at any
instant from a union. What does this actually mean ? I cannot use two
members simultaneously in a single statement ?



在一个联合中,分配的空间等于最大成员的大小

(以及实现完成的任何填充)。

union _foo {

int bar;

double baz;

} foo;


sizeof(union foo)会给你sizeof(double)和一些填充。

bar和baz共享相同的内存空间。他们是不同的
成员,但他们共享相同的记忆空间。


考虑:


#define DOUBLE 1

#define STRING 2

#define INT 3

struct _node {

int type;

union {

double d;

char a [10];

int i;

}

_node * next;

}节点;

struct node * list;


/ * ... * /

void * getElement(void){

switch(list-> type){

case DOUBLE:

return&(list-> d);

case STRING:

return list-> a;

案例INT:

return&(list-> i);

默认值:

返回null;

}


这只是一个骨架。在这种情况下,您正在使用工会

有一个hetrogenous列表。还有其他一些场景,但我不认为值得为了节省空间而与工会合作。

In an union, the space allocated equals the size of biggest member
(and any padding done by the implementation).
union _foo {
int bar;
double baz;
}foo;

sizeof(union foo) will give you sizeof(double) and maybe some padding.
Both bar and baz share the same memory space. They are distinct
members but they share the same memory space.

Consider :

#define DOUBLE 1
#define STRING 2
#define INT 3

struct _node {
int type;
union {
double d;
char a[10];
int i;
}
_node *next;
}node;
struct node *list;

/* ... */
void *getElement(void) {
switch (list->type) {
case DOUBLE:
return &(list->d);
case STRING:
return list->a;
case INT:
return &(list->i);
default:
return null;
}

This is just a skeleton. In this scenario, you are using unions to
have a hetrogenous list. There are some other scenarios but I don''t
think its worth playing tricks with unions just to save space.


rahul写道:


< snip>
rahul wrote:

<snip>

在一个工会中,分配的空间等于最大成员的大小

(以及实现完成的任何填充)。

union _foo {

int bar;

double baz;

} foo;


sizeof(union foo)会给你sizeof(double)和一些填充。
In an union, the space allocated equals the size of biggest member
(and any padding done by the implementation).
union _foo {
int bar;
double baz;
}foo;

sizeof(union foo) will give you sizeof(double) and maybe some padding.



如果我错了,请纠正我,但是对于具有sizeof(int)的

实现来说,它是否完全符合要求sizeof(双)?

< snip>

Correct me if I''m wrong, but isn''t it perfectly conforming for an
implementation to have sizeof(int) sizeof(double)?

<snip>


这篇关于正确使用工会。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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