按位复制在这里? [英] bitwise copying here?

查看:93
本文介绍了按位复制在这里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,赋值z = y会按位进行复制吗?


struct x

{

int a;

int b;

int c;

};


main()< br $>
{

struct xz,y;

...

z = y;

}

解决方案

webfan写道:


在下面的代码中,赋值z = y会按位进行复制吗?


struct x

{

int a;

int b;

int c;

};


main()

{

struct xz,y;

...

z = y;


}



可能,但不能保证。

保证的是你有效$

za = ya;

zb = yb;

zc = yc;


如果有两个可能的代表其中一个值的tations,

然后赋值可能会改变表示。


如果由于某些奇怪的原因,struct x有填充字节,那么

a,b和c的表示可能相同,而填充字节的

表示不同。


如果你想逐位复制,请使用

memcpy(& z,& y,sizeof z);

干杯

Michael

-

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


< blockquote>

Michael Mair写道:


如果你想逐位复制,请使用

memcpy(& z,& y,sizeof z);



memcpy逐字节复制而不是逐位复制


-kondal


kondal写道:


Michael Mair写道:


& gt;>如果你想要逐位复制,请使用
memcpy(& z,& y,sizeof z);



memcpy执行逐字节复制而不是逐位



memcpy()可以逐字节复制或逐位复制,或者

nybble-by-nybble,或者别的什么。该标准仅表示它复制了n个字符,即复制n个字符。但是没有说明如何复制这些字符

。实际上,

这允许memcpy()实现复制四字节字

或八字节八字或其他大于字节大小的单位,

如果可以盈利的话。


从严格符合计划的角度来看,

的差异(如果有一个是微妙的,因为

是不可察觉的隐形。


-

Eric Sosman
es*****@acm-dot-org.inva


In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}

解决方案

webfan wrote:

In the code below, will the assignment z= y do bitwise copying?

struct x
{
int a;
int b;
int c;
};

main()
{
struct x z, y;
...
z = y;

}

Probably, but not guaranteedly.
What is guaranteed is that you have the effect of
z.a = y.a;
z.b = y.b;
z.c = y.c;

If there are two possible representations for one of these values,
then the assignment may change the representation.

If, for some strange reason, struct x has padding bytes, then
the representation of a, b, and c may be the same while the
representation of the padding bytes is different.

If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.



Michael Mair wrote:

If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);

memcpy does a byte-by-byte copy not bit-by-bit

-kondal


kondal wrote:

Michael Mair wrote:

>>If you want a bit-by-bit copy, use
memcpy(&z, &y, sizeof z);


memcpy does a byte-by-byte copy not bit-by-bit

memcpy() may do a byte-by-byte copy, or bit-by-bit, or
nybble-by-nybble, or something else. The Standard says only
that it "copies n characters," but says nothing about how
those characters are to be copied. As a practical matter,
this allows a memcpy() implementation to copy four-byte words
or eight-byte octawords or other larger-than-byte-sized units,
if it can profitably do so.

From the point of view of a strictly conforming program,
the difference (if there is one) is subtle to the point of
being undetectably invisible.

--
Eric Sosman
es*****@acm-dot-org.invalid


这篇关于按位复制在这里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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