联盟问题 [英] Union Issue

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

问题描述

亲爱的所有人


我们知道,当我们申报工会时,我们有

联盟的大小,这是最高数据的大小类型如下面的情况



大小应为4(对于我的情况和编译器),它是,


我从下面的代码中得出结论


union data_type {


unsigned int a;

char b [ 2];

};


< --------------无符号整数------ ------------>

---------------------------- -------------------

| | | | |

----------------------------------------- -------

< - b [0] - >< --- b [1] --->

int main()


{

union data_type d;

da = 512 * 512;

printf("%d \ n",da);

printf("%d \ n%d \ n%d \ n%d \ n",db [0],db [1],db [2],db [3]);

getchar();

返回0;

}


当我写上面的代码时,它意味着

< -------------- Unsigned int a -------->

------------------------------- ------

| 00000000 | 00000000 | 00000010 | 0000000 |

------------------- ------------------

< - b [0] - >< - b [1] - >

现在,当我特定地为两个字节数组创建char时,


那么为什么在获取它时获取b [2]和b [3] <的值br />
char的内部填充是否也在联合中进行。

那是4个字节的字符串, Union还支持

填充吗? (填充是实现使用)


请让我知道发生了什么以及我理解错了什么?


问候

Ranjeet

解决方案

它不是填充。当你打印一个字符串时,应该用ascii null字符终止

。在你的情况下,它可能不存在。


ra *********** @ gmail.com 写道:


亲爱的所有

As我们知道当我们宣布联合时,我们有
联合的大小,这是最高数据类型的大小,如下面的情况

大小应该是4(对于我案例和编译器),它是,


联合的大小可能大于它的最大数据类型。

尝试char b [ 5];而不是char b [2];

然后读取sizeof d。

我从下面的代码得出的结论

union data_type {

unsigned int a;
char b [2];
};
int main()

联合数据类型d;
da = 512 * 512;
printf("%d \ n", da);
printf("%d \ n%d \ n%d \ n%d \ n",db [0],db [1],db [2],db [3]);
getchar();
返回0;
}
现在,当我特定地为两个字节数组创建char时,

然后为什么在获取它时获取b [2]和b [3]
的值

没有b [2]和b [3]。你的代码是未定义的。

char的内部填充也是在union中进行的。
那是4字节的char,是否还支持
填充? (填充是实施用途)

请让我知道发生了什么以及我理解错了什么?




工会的目的不是为了阅读

与最后写的不同类型。


N869

6.5.2.3结构和工会成员

[#5]除了一个例外情况,如果对象的最近一个商店

是使用了一个联盟成员

对象的值一个不同的成员,

行为是实现定义的
。一个特殊的保证是在

命令中简化联合使用:如果一个联合包含

几个共享一个共同初始序列的结构(参见
$ b)如果联盟对象目前包含

这些结构之一,则允许在声明的任何地方检查其中任何一个的常见

初始部分

完整的工会类型可见。


-

pete


"马达夫" <毫安*********** @ gmail.com>写道:

它不是填充。当你打印一个字符串时,它应该由ascii null字符终止。在你的情况下,它可能不存在。




请在发布后续内容时提供一些背景信息。不要假设

每个人都可以看到父文章。


如果你想通过groups.google.com发布一个后续内容,请不要使用

破损的回复链接在文章的底部。点击

" show options"在文章的顶部,然后点击

回复在文章标题的底部。


无论如何,原始文章中的问题无关

字符串终止。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Dear ALL

As we know that when we declare the union then we have the size of the
union which is the size of the highest data type as in the below case
the
size should be 4 (For my case and compiler), and it is,

what I conclude from the below code

union data_type {

unsigned int a;
char b[2];
};

<--------------Unsigned int a------------------>
-----------------------------------------------
| | | | |
------------------------------------------------
<--b[0]--><---b[1]--->
int main ()

{
union data_type d;
d.a = 512 * 512;
printf("%d\n", d.a);
printf("%d\n %d\n %d\n %d\n ", d.b[0],d.b[1],d.b[2],d.b[3]);
getchar();
return 0;
}

when i write the above code the it means that
<--------------Unsigned int a-------->
-------------------------------------
|00000000|00000000|00000010|0000000|
-------------------------------------
<--b[0]-><--b[1]->
Now when I specificaaly Allcoate the char for two bytes array,

then why on fetching its is fetching the value of b[2] and b[3]
Is the internal padding for the char is also going on in union.
thats is making to the 4 bytes char, Does Union also support the
padding ? (Padding is implementation use)

Please let me know what is happening and what I understood wrong ?

Regards
Ranjeet

解决方案

Its not the padding. when you print a string, it should be terminated
by an ascii null character. In your case , it may not have been there.


ra***********@gmail.com wrote:


Dear ALL

As we know that when we declare the union then we have the size of the
union which is the size of the highest data type as in the below case
the
size should be 4 (For my case and compiler), and it is,
The size of the union may be larger than it''s largest data type.
Try char b[5]; instead of char b[2];
and then read sizeof d.
what I conclude from the below code

union data_type {

unsigned int a;
char b[2];
}; int main ()

{
union data_type d;
d.a = 512 * 512;
printf("%d\n", d.a);
printf("%d\n %d\n %d\n %d\n ", d.b[0],d.b[1],d.b[2],d.b[3]);
getchar();
return 0;
} Now when I specificaaly Allcoate the char for two bytes array,

then why on fetching its is fetching the value of b[2] and b[3]
There is no b[2] and b[3]. Your code is undefined.
Is the internal padding for the char is also going on in union.
thats is making to the 4 bytes char, Does Union also support the
padding ? (Padding is implementation use)

Please let me know what is happening and what I understood wrong ?



The purpose of unions is not to read a
different type than the one last written.

N869
6.5.2.3 Structure and union members
[#5] With one exception, if the value of a member of a union
object is used when the most recent store to the object
was to a different member, the
behavior is
implementation-defined. 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 completed type of the union is visible.

--
pete


"Madhav" <ma***********@gmail.com> writes:

Its not the padding. when you print a string, it should be terminated
by an ascii null character. In your case , it may not have been there.



Please provide some context when you post a followup. Don''t assume
that everyone can see the parent article.

If you want to post a followup via groups.google.com, don''t use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

In any case, the problem in the original article has nothing to do
with string termination.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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