工会不安全的例子? [英] An example of unions not being type safe?

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

问题描述

好吧,就像最近一样,在C中使用Union的整个想法终于沉入了我的头骨。说真的,我认为可能花了我2年的时间才能确定联盟的实际情况。姗姗来迟,我也提到了这个我很聪明的朋友,他刚刚辞去了无线公司的CTO职务,因此他完成了粒子物理学博士学位。无论如何,他提到了B $中的联盟不是类型安全的。


现在,如何在联盟中违反类型安全?


Chad

解决方案



" Chad" < cd ***** @ gmail.comwrote in message

news:11 ********************** @ q3g2000prf。 googlegro ups.com ...


好​​的,好像最近在C中使用Union的整个想法

沉入我的头骨。说真的,我认为可能花了我2年的时间才能确定联盟的实际情况。姗姗来迟,我也提到了这个我很聪明的朋友,他刚刚辞去了无线公司的CTO职务,因此他完成了粒子物理学博士学位。无论如何,他提到了B $中的联盟不是类型安全的。


现在,如何在联盟中违反类型安全?



#include< stdio.h>


int main(void){

union {

int i;

double d;

} z;


zd = 42.7 ;

printf("%d \ nn",zi); / *哎呀* /

返回0;

}


-

poncho
blockquote>

8月15日下午1:17,Scott Fluhrer < sfluh ... @ ix.netcom.comwrote:


" Chad" < cdal ... @ gmail.comwrote in message


现在,如何在联盟中违反类型安全?



z.d = 42.7;

printf("%d \ n",z.i); / *糟糕* /



嗯,你不能这样做。你会不会说
也说int不是类型安全的因为

你可以写:


int x = 10;

printf("%f",*(double *)& x); / * oops * /


?如果你遵守规定你所允许的工会是什么,那么我就不会看到

任何违反类型安全的行为。


老狼说:


8月15日下午1:17,Scott Fluhrer < sfluh ... @ ix.netcom.comwrote:


>"" Chad" < cdal ... @ gmail.comwrote in message


现在,如何在联盟中违反类型安全?


z.d = 42.7;
printf("%d \ n",z.i); / *糟糕* /



嗯,你不能这样做。



错误。该行为是在C90中实现定义的,并且在
C99中未定义。利用这两种行为都是*禁止*,尽管

它确实使你的程序不可移植。我不是以任何方式纵容上述

,但是你不能这样做并不是真的,

不止于此确实,你不允许定义一个数组

,其中1<< CHAR_BIT元素(实现定义的行为)或

取消引用指向MS-DOS下的视频内存的指针(未定义

行为)。


你会说

还说int不是类型安全的,因为

你可以写:


int x = 10;

printf("%f",*(double *)& x); / * oops * /



是的。打破类型系统的原因是多种类型存在多种类型以及它们之间的转换能力。


< snip>


-

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

电邮:-www。 + rjh @

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

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


Okay, so like recently the whole idea of using a Union in C finally
sunk into my skull. Seriously, I think it probably took me 2 years to
catch on what a Union really is. Belated, I mentioned this too my
ultra smart friend who just quit working as a CTO of a wireless
company so he go complete his PhD in particle physics. Anyhow he
mentioned that Unions in C are not typesafe.

Now, how is it possible to violate type safety in Unions?

Chad

解决方案


"Chad" <cd*****@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...

Okay, so like recently the whole idea of using a Union in C finally
sunk into my skull. Seriously, I think it probably took me 2 years to
catch on what a Union really is. Belated, I mentioned this too my
ultra smart friend who just quit working as a CTO of a wireless
company so he go complete his PhD in particle physics. Anyhow he
mentioned that Unions in C are not typesafe.

Now, how is it possible to violate type safety in Unions?

#include <stdio.h>

int main(void) {
union {
int i;
double d;
} z;

z.d = 42.7;
printf( "%d\n", z.i ); /* Oops */
return 0;
}

--
poncho


On Aug 15, 1:17 pm, "Scott Fluhrer" <sfluh...@ix.netcom.comwrote:

"Chad" <cdal...@gmail.comwrote in message

Now, how is it possible to violate type safety in Unions?


z.d = 42.7;
printf( "%d\n", z.i ); /* Oops */

Well, you aren''t allowed to do that. Would you
also say that an int is not typesafe because
you can write:

int x = 10;
printf("%f", *(double *)&x); /* oops */

? If you follow the rules as to what you are
permitted to do with unions, then I don''t see
any violation of type safety.


Old Wolf said:

On Aug 15, 1:17 pm, "Scott Fluhrer" <sfluh...@ix.netcom.comwrote:

>"Chad" <cdal...@gmail.comwrote in message

Now, how is it possible to violate type safety in Unions?


z.d = 42.7;
printf( "%d\n", z.i ); /* Oops */


Well, you aren''t allowed to do that.

False. The behaviour is implementation-defined in C90, and undefined in
C99. Taking advantage of either behaviour is *not* forbidden, although
it does render your program non-portable. I''m not condoning the above
by any means, but it is not true that "you aren''t allowed to do that",
any more than it is true that you are not allowed to define an array
with 1 << CHAR_BIT elements (implementation-defined behaviour) or
dereference a pointer to video memory under MS-DOS (undefined
behaviour).

Would you
also say that an int is not typesafe because
you can write:

int x = 10;
printf("%f", *(double *)&x); /* oops */

Yes. What makes it possible to break the type system is the existence of
multiple types and the ability to convert between them.

<snip>

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


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

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