FAQ-问题 [英] FAQ-Question

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

问题描述




我偶然发现以下代码确定FAQ中的字节顺序:


union {

int i;

char c [sizeof(int)];

} x;

/ * do stuff * /


在这种情况下,似乎联盟用于将内存部分转换为
从int转换为char []。但是我已经知道你不能使用工会来重新解释一个记忆区域,因为你不能总是确定

字段真的如此使用相同的职位。那么为什么这个代码便携?


谢谢,

直到


-

请添加盐和胡椒到主题行绕过我的垃圾邮件过滤器

解决方案

Till Crueger写道:


union {
int i;
char c [sizeof(int)];
} x;
/ *做的东西* /

在这种情况下,似乎联盟用于将内存部分从int转换为char [] 。但是我已经知道你不能使用工会来重新解释一个记忆区域,因为你不能总是确定
字段确实使用相同的位置。那么为什么这个代码是可移植的呢?




它不是便携式的,但不是你提到的原因。在联合的元素之前不能填充
,联盟的第一个字节是每个成员的第一个字节,总是如此。问题是

检索union元素的值仅在检索到的成员

是最近分配的成员时定义,因此分配i和

然后检查c未定义。


Robert Gamble


" Till Crueger" <钛**** @ gmx.net>在消息中写道

news:pa **************************** @ gmx.net ... < blockquote class =post_quotes>

我偶然发现以下代码来确定FAQ中的字节顺序:

union {
int i;
char c [sizeof(int)];
} x;
/ *做东西* /

在这种情况下,联盟似乎用于转换内存从int到char []的部分
。但是我已经知道你不能使用工会来重新解释一个记忆区域,因为你不能总是确定
字段确实使用相同的位置。那么为什么这段代码可以移植?


来自:ISO / IEC 9899:1999(E)(这是现行标准)我们有这个:

5一个特殊保证是按顺序简化联合的使用:如果一个

联合包含几个共享一个共同初始序列的结构(参见下面的
),并且如果union对象当前包含其中一个结构,

允许检查其中任何一个的共同初始部分

,可以看到完整类型的联合声明。两个

结构共享一个共同的初始序列,如果相应的成员具有

兼容类型(并且,对于位字段,相同的宽度),对于一系列

一个或多个初始会员。

谢谢,
直到

-
请添加盐和胡椒到主题行绕过我的垃圾邮件过滤器



Till Crueger< Ti **** @ gmx.net>写道:

union {
int i;
char c [sizeof(int)];


unsigned char c [sizeof(int)];


我很惊讶它显然不是那样的常见问题。

} x;
/ *做东西* /
请添加盐和胡椒 $}
您可以随时使用Google邮件:-)

-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


Hi,

I stumbled upon the following code to determine byte ordering in the FAQ:

union {
int i;
char c[sizeof(int)];
} x;
/* do stuff */

In this case it seems that the union was used to convert a memory section
from int to char[]. However I''ve learned that you couldn''t use unions to
reinterpret a memory area, because you can''t always be sure that the
fields realy do use the same positions. So why is this code portable?

Thanks,
Till

--
Please add "Salt and Pepper" to the subject line to bypass my spam filter

解决方案

Till Crueger wrote:

Hi,

I stumbled upon the following code to determine byte ordering in the FAQ:

union {
int i;
char c[sizeof(int)];
} x;
/* do stuff */

In this case it seems that the union was used to convert a memory section
from int to char[]. However I''ve learned that you couldn''t use unions to
reinterpret a memory area, because you can''t always be sure that the
fields realy do use the same positions. So why is this code portable?



It''s not portable but not for the reason you mentioned. There cannot
be padding before the elements of a union, the first byte of a union is
the first byte of each of its members, always. The problem is that
retrieving the value of a union element is only defined when the member
retrieved was the member most recently assigned, so assigning i and
then examining c is not defined.

Robert Gamble


"Till Crueger" <Ti****@gmx.net> wrote in message
news:pa****************************@gmx.net...

Hi,

I stumbled upon the following code to determine byte ordering in the FAQ:

union {
int i;
char c[sizeof(int)];
} x;
/* do stuff */

In this case it seems that the union was used to convert a memory section
from int to char[]. However I''ve learned that you couldn''t use unions to
reinterpret a memory area, because you can''t always be sure that the
fields realy do use the same positions. So why is this code portable?
From : ISO/IEC 9899:1999 (E) (which is the current standard) we have this:
5 One special guarantee is made in order to simplify the use of unions: if a
union contains several structures that share a common initial sequence (see
below), and if the union object currently contains one of these structures,
it is permitted to inspect the common initial part of any of them anywhere
that a declaration of the complete type of the union is visible. Two
structures share a common initial sequence if corresponding members have
compatible types (and, for bit-fields, the same widths) for a sequence of
one or more initial members.
Thanks,
Till

--
Please add "Salt and Pepper" to the subject line to bypass my spam filter



Till Crueger <Ti****@gmx.net> wrote:

union {
int i;
char c[sizeof(int)];
unsigned char c[sizeof(int)];

I''m surprised that it''s apparently not like that in the FAQ.
} x;
/* do stuff */ Please add "Salt and Pepper" to the subject line to bypass my spam filter



You could always just use Google mail :-)

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于FAQ-问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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