结构 - 分配? [英] structures --assignment?

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

问题描述

你好,

我有一个简单的(newbee)问题。

以下是正确的吗?


struct x {

int a;

int b;

}

...


struct x test1;

struct x test2;

[.. init test1以某种方式]


test2 = test1


我认为FAQ中有一些内容,但是

我不确定我是否采取了正确的措施。上面的

代码适用于我正在使用的编译器,但是

我很确定是否允许使用这种语句。

感谢您的帮助

Michael

Hi there,
I''ve got a simple (newbee)-question.
Is the following thing right?

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

struct x test1;
struct x test2;
[.. init test1 somehow]

test2 = test1

I think there is something in the FAQ, but
I''m not sure if I took it right. The above
code works with the compiler I''m using, but
I''m sure if this kind of statement is allowed.
Thanks for any help
Michael

推荐答案

" Michael" < MI **** @ gmx.net>在消息中写道

news:bu ********** @ rzcomm2.rz.tu-bs.de ...
"Michael" <mi****@gmx.net> wrote in message
news:bu**********@rzcomm2.rz.tu-bs.de...
struct x {
int a;
int b;
}
; / *别忘了:) * / ...

struct x test1;
struct x test2;
[... init test1以某种方式]

test2 = test1
struct x {
int a;
int b;
} ; /* Don''t forget :) */ ...

struct x test1;
struct x test2;
[.. init test1 somehow]

test2 = test1




是的,结构分配很好。但是如果你在结构中有指针,那么你可能需要复制指向的数据(所谓的深度

副本)。 (另一种选择是引用计数,但是这更复杂了

复杂。)



Yes, structure assignment is fine. But if you have pointers in the struct,
you might need to make copies of the data pointed to (a so-called "deep
copy"). (An alternative is reference counting, but that''s a bit more
complicated.)


Michael< mi **** @ gmx达网络>写道:
Michael <mi****@gmx.net> wrote:
以下是对的吗?

struct x test1;
struct x test2;

test2 = test1
Is the following thing right?

struct x test1;
struct x test2;

test2 = test1




当然,完全正确。


但是请注意,没有指定复制是否完成

按字节或每个成员;如果结构包含填充,则

memcmp(& test1,& test2,sizeof test1)可能会或可能不会成功,具体取决于您实现的
。不过,所有个人会比较平等。


Richard



Sure, perfectly correct.

Note, though, that it is not specified whether the copying is done
bytewise or per member; if the struct contains padding, then
memcmp(&test1, &test2, sizeof test1) may or may not succeed depending on
your implementation. All individual members will compare equal, though.

Richard


嘿,

谢谢你快速的答案。所以我不再需要b $ b打扰了。


@Alex:Mea culpa,但代码没有被取消
$ b一些节目$ b ;-)

祝你有愉快的一天

Michael
Hey,
thank you for the fast answers. So I don''t
bother anymore.

@Alex: Mea culpa, but whew the code wasn''t taken
out of some program ;-)

Have a nice day
Michael


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

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