关于工会的问题 [英] question on union

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

问题描述

您好,


我正在通过UNIX网络编程通过R.Stevens并坚持使用

以下代码,确定正在运行的主机的endiannes:


#include< stdio.h>

#include< stdlib.h>


#define CPU_VENDOR_OS" i686-pc-linux-gnu"


int main(无效)

{

union {

short s;

char c [sizeof (短)];

}联合国;


un.s = 0x0102;

printf("%s:" ;,CPU_VENDOR_OS);

if(sizeof(short)== 2){

if(un.c [0] == 1&& un.c [1] == 2)

printf(" big-endian \ n");

else if(un.c [0] == 2& & un.c [1] == 1)

printf(" little-endian \ n");

else

printf(unknown\\\
);

}其他

printf(" sizeof(短)=%d \ n",sizeof(短)) ;


退出(0);

}


我没有得到的是怎么样的un.c [0]和un.c [1]都包含

已被初始化的内容,即0x0102。它是''union''的一个特性吗?

为什么我们不能用''struct'来检查字节在内存中的放置方式?


提前致谢!

最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru

Hello,

I''m going through the "UNIX network programming" by R.Stevens and stuck with
the following code, determining the endiannes of a host it is running on:

#include <stdio.h>
#include <stdlib.h>

#define CPU_VENDOR_OS "i686-pc-linux-gnu"

int main(void)
{
union {
short s;
char c[sizeof(short)];
} un;

un.s = 0x0102;
printf("%s: ", CPU_VENDOR_OS);
if (sizeof(short) == 2) {
if (un.c[0] == 1 && un.c[1] == 2)
printf("big-endian\n");
else if (un.c[0] == 2 && un.c[1] == 1)
printf("little-endian\n");
else
printf("unknown\n");
} else
printf("sizeof(short) = %d\n", sizeof(short));

exit(0);
}

What I don''t get is how come that un.c[0] and un.c[1] both contain what has
been un.s initialized, i.e. 0x0102. Is it a feature of ''union''?
Why could not we use ''struct'' to check how bytes are placed in memory ?

Thanks in advance!
With best regards, Roman Mashak. E-mail: mr*@tusur.ru

推荐答案

Roman Mashak写道:
Roman Mashak wrote:

我不知道的是un.c [0]和un.c [1]都包含了什么? />
已被初始化,即0x0102。它是''union''的一个特性吗?

为什么我们不能用''struct'来检查字节在内存中的放置方式?
What I don''t get is how come that un.c[0] and un.c[1] both contain what has
been un.s initialized, i.e. 0x0102. Is it a feature of ''union''?
Why could not we use ''struct'' to check how bytes are placed in memory ?



结构中的元素都占据独立且不同的元素

件记忆,但联盟的元素都占据了一个共同的片段。记忆。


-

Morris Dovey

DeSoto Solar

德索托,爱荷华州美国
http://www.iedu.com/DeSoto


你好,莫里斯!

你写的是2008年2月12日星期二20:32:28 -0500:


??>我没有得到的是un.c [0]和un.c [1]都包含

??> un.s初始化的内容,即0x0102。这是

??&'''union'的特色吗?为什么我们不能使用''struct'来检查字节的放置方式

??>在内存中?


MD结构中的元素全部占用单独和不同

MD" pieces"记忆,但联盟的元素都占据了一个名为
的MDcommon片段。记忆。

背后的机制是什么?比如,在发布的示例中,在运行时un.s =

0x0102并且该值(0x0102)占用一些公共内存。根据体系结构,在内存中放置un.c值是不是CPU谁负责?

那么为什么在调试器中这两个值看起来不同:


(gdb)p / x un
Hello, Morris!
You wrote on Tue, 12 Feb 2008 20:32:28 -0500:

??>What I don''t get is how come that un.c[0] and un.c[1] both contain
??>what has been un.s initialized, i.e. 0x0102. Is it a feature of
??>''union''? Why could not we use ''struct'' to check how bytes are placed
??>in memory ?

MDThe elements in a structure all occupy separate and distinct
MD"pieces" of memory, but the elements of a union all occupy a
MDcommon "piece" of memory.
What is the mechanics behind that? Say, in posted example at run-time un.s =
0x0102 and this value (0x0102) occupies some common memory. Is it CPU who is
in charge to lay out un.c value in the memory according to architecture?
Then why do both values look differently, in debugger:

(gdb) p/x un


3 = {s = 0x102,c = {0x2,0x1}}

(gdb)


最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru
3 = {s = 0x102, c = {0x2, 0x1}}
(gdb)

With best regards, Roman Mashak. E-mail: mr*@tusur.ru


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

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