关于struct copy的2个问题 [英] 2 questions about struct copy

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

问题描述

我被2个关于结构复制的问题困扰着。虽然我搜索了

网,但仍然处于困惑状态。


1.结构分配是否复制了包括数组成员在内的每个成员?

例如,


struct A

{

int n;

int m [2 ];

};


struct A a = {1,{2,3}};

struct A b = {7,{8,9}};


在b = a之后; b.m [0]和b.m [1]的值是否与a相同?我用bingw测试了这个
,答案是肯定的。但我想知道

这种行为是否符合标准。


2.结构赋值是否等于memcpy?

如果是这样,那么在结构赋值后,对它们应用memcmp将

返回0(相等);否则memcmp将返回非零。


任何指导?

Hi, I''m haunted by 2 questions about struct copy. Though I searched the
net, but still in confusion.

1. Does struct assignment copies every member including array members?
For example,

struct A
{
int n;
int m[2];
} ;

struct A a = { 1, {2,3}};
struct A b = { 7, {8,9}};

After b = a; will b.m[0] and b.m[1] have the same value as in a? I
tested this using mingw, and the answer is yes. But i want to know if
this behavior is guaranteed by standard.

2. Does struct assignment equal to memcpy them?

If so, then after struct assignment, to apply memcmp on them will
return 0(equal); otherwise memcmp will return non-zero.

Any guidance?

推荐答案

这里< a rel =nofollowhref =http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html\"target =_ blank> http://www.le.ac.uk/cc /tutorials/c/ccccstrc.html 是一个很好的来源

关于结构复制。

Here http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html is a good source
about struct copying.


WaterWalk说:
WaterWalk said:
我被2个关于struct copy的问题所困扰。虽然我搜索了网页,但仍然处于混乱状态。

1.结构分配是否会复制包括数组成员在内的每个成员?


是。

2.结构分配是否等于memcpy?
Hi, I''m haunted by 2 questions about struct copy. Though I searched the
net, but still in confusion.

1. Does struct assignment copies every member including array members?
Yes.
2. Does struct assignment equal to memcpy them?




目前为止正如我所知,标准并没有这样说。但是,我们可以确保每个成员都被正确复制。一个结构可以包含

填充位或甚至填充字节,我也看不出任何保证

这些也会被复制(虽然在实践中它很可能他们将会是b $ b。


-

理查德希思菲尔德

" Usenet is一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



As far as I can make out, the Standard doesn''t say this. We can, however, be
assured that each member is correctly copied. A structure can contain
padding bits or even padding bytes, and I can''t see any guarantee that
these would be copied too (although in practice it is very likely that they
would be).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


" WaterWalk" <到******** @ 163.com>写道:
"WaterWalk" <to********@163.com> writes:
我被2个关于struct copy的问题所困扰。虽然我搜索了网页,但仍处于混乱状态。

1.结构分配是否复制了包括阵列成员在内的每个成员?
例如,

struct A
{
int n;
int m [2];
};

struct A a = {1,{2,3}结构A b = {7,{8,9}};

b = a之后; b.m [0]和b.m [1]的值是否与a相同?我用mingw测试了这个,答案是肯定的。但我想知道
这种行为是否符合标准。


是。

2.结构赋值是否等于memcpy?

如果是,那么在结构赋值后,申请对他们的memcmp将返回0(相等);否则memcmp将返回非零。
Hi, I''m haunted by 2 questions about struct copy. Though I searched the
net, but still in confusion.

1. Does struct assignment copies every member including array members?
For example,

struct A
{
int n;
int m[2];
} ;

struct A a = { 1, {2,3}};
struct A b = { 7, {8,9}};

After b = a; will b.m[0] and b.m[1] have the same value as in a? I
tested this using mingw, and the answer is yes. But i want to know if
this behavior is guaranteed by standard.
Yes.
2. Does struct assignment equal to memcpy them?

If so, then after struct assignment, to apply memcmp on them will
return 0(equal); otherwise memcmp will return non-zero.




最有可能,但不保证。结构可以包含成员之间和/或最后一个成员之后的间隔
。分配不是
保证复制这些差距的内容。完成任务后,

逐个成员的比较会成功,但memcmp()可能会失败。

(执行复制可能更容易)整个

结构比跳过差距。所以这不可能发生在

练习中。


我认为*这是正确的,但我无法在标准中找到

最终声明。


-

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

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

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



Most likely it will, but it''s not guaranteed. Structures can contain
gaps between members and/or after the last member. Assignment isn''t
guaranteed to copy the contents of those gaps. After an assignment, a
member-by-member comparison will succeed, but memcmp() could fail.
(it''s probably easier for the implementation to copy the entire
structure than to skip over the gaps. so this isn''t likely to occur in
practice).

I *think* this is correct, but I haven''t been able to track down a
definitive statement in the standard.

--
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.


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

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