打字 [英] Type punning

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

问题描述

这:

#include< stdint.h>


union r_float {

float f;

uint32_t i;

};


/ * .. * /

float f;

uint32_t i;

union r_float ra;


/ * .. * /

ra.f = f;

i = ra.i;


仅取决于实现定义的方面,对吧?也就是说,如果我知道

uint32_t存在并且对象的大小相同,那么''我'和'''''/ b $ b'b'具有相同的位模式作业?同样,同样的

持有:


ra.i = i;

f = ra.f;


如果我知道我的位模式不是浮动的陷阱表示?


S.

This:
#include <stdint.h>

union r_float {
float f;
uint32_t i;
};

/* .. */
float f;
uint32_t i;
union r_float ra;

/* .. */
ra.f = f;
i = ra.i;

depends only on implementation-defined aspects, right? That is, if I know
uint32_t exists and the sizes of the objects are identical, will ''i'' and ''f''
have identical bit patterns after the assignments? Similarly, will the same
hold for:

ra.i = i;
f = ra.f;

if I know the bit pattern of ''i'' is not a trap representation for ''float''?

S.

推荐答案

Skarmander写道:
Skarmander wrote:
这个:
#include< stdint.h>
union r_float {
float f;
uint32_t i;
};

/ * .. * /
浮动f;
uint32_t我;
联盟r_float ra;

/ * .. * /
ra.f = f;
我= ra.i;

仅取决于实现定义的方面,对吧?也就是说,如果我知道uint32_t存在并且对象的大小相同,那么i和f在分配后是否会有相同的位模式?同样,相同的
将持有:


标准不保证相同大小的对象将会重叠位b / b 。如果您的实现确保它们将是b $ b,并且您知道对象的大小相同,并且为一个成员指定的

值代表另一个成员的有效值

成员然后就我所见,没有未定义的行为。

ra.i = i;
f = ra.f;
<如果我知道''i'的位模式不是''float''的陷阱表示?
This:
#include <stdint.h>

union r_float {
float f;
uint32_t i;
};

/* .. */
float f;
uint32_t i;
union r_float ra;

/* .. */
ra.f = f;
i = ra.i;

depends only on implementation-defined aspects, right? That is, if I know
uint32_t exists and the sizes of the objects are identical, will ''i'' and ''f''
have identical bit patterns after the assignments? Similarly, will the same
hold for:
It is not guaranteed by the Standard that objects of the same size will
overlap bit for bit. If your implementation does guarantee that they
will and you know that the objects are the same size and that the
values specified for one member represent a valid value for the other
member then there is no undefined behavior as far as I can see.
ra.i = i;
f = ra.f;

if I know the bit pattern of ''i'' is not a trap representation for ''float''?




与上面相同。


Robert Gamble



Same as above.

Robert Gamble


Skarmander写道:
Skarmander wrote:
这个:
#include< ; stdint.h>

union r_float {
float f;
uint32_t i;
};

/ * .. * /
浮动f;
uint32_t i;
联合r_float ra;

/ * .. * /
ra.f = f;
i = ra.i;

仅取决于实现定义的方面,对吧?也就是说,如果我知道uint32_t存在并且对象的大小相同,那么在分配后,
我和f会有相同的位模式吗?
同样同样坚持:

ra.i = i;
f = ra.f;

如果我知道''我'的位模式'不是''浮动''的陷阱表示?
This:
#include <stdint.h>

union r_float {
float f;
uint32_t i;
};

/* .. */
float f;
uint32_t i;
union r_float ra;

/* .. */
ra.f = f;
i = ra.i;

depends only on implementation-defined aspects, right? That is, if I
know uint32_t exists and the sizes of the objects are identical, will
''i'' and ''f'' have identical bit patterns after the assignments?
Similarly, will the same hold for:

ra.i = i;
f = ra.f;

if I know the bit pattern of ''i'' is not a trap representation for ''float''?




只要没有任何填充位或填充位

是相同的。


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



As long as there are no padding bits whatsoever or if the padding bits
are the same.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Michael Mair写道:
Michael Mair wrote:
Skarmander写道:
Skarmander wrote:
这个:
#include< stdint.h>

union r_float {
float f;
uint32_t i;
};

/ * .. * /
浮动f;
uint32_t i;
联合r_float ra;

/ * .. * /
ra.f = f;
i = ra.i;

仅取决于实现定义的方面,对吧?也就是说,如果我知道uint32_t存在并且对象的大小相同,那么在分配后,
我和f会有相同的位模式吗?
同样同样坚持:

ra.i = i;
f = ra.f;

如果我知道''我'的位模式'不是'浮动''的陷阱表示?
This:
#include <stdint.h>

union r_float {
float f;
uint32_t i;
};

/* .. */
float f;
uint32_t i;
union r_float ra;

/* .. */
ra.f = f;
i = ra.i;

depends only on implementation-defined aspects, right? That is, if I
know uint32_t exists and the sizes of the objects are identical, will
''i'' and ''f'' have identical bit patterns after the assignments?
Similarly, will the same hold for:

ra.i = i;
f = ra.f;

if I know the bit pattern of ''i'' is not a trap representation for
''float''?



只要没有任何填充位或者填充位是否相同。



As long as there are no padding bits whatsoever or if the padding bits
are the same.



uint32_t根据定义没有填充位;对于''浮点''和填充位

未定义,因为整个表示未指定,但是这个

应该由无陷阱表示覆盖;


S.


uint32_t has no padding bits, by definition; for a ''float'' "padding bits"
are not defined, since the entire representation is unspecified, but this
should be covered by "no trap representations".

S.


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

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