复制结构成员 [英] Copying structure members

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

问题描述

我和结构比较迷茫。有许多方法来复制的结构。

I am more confused with structures. There are many ways to copy a structure.

struct complex
{
   int real;
   int imaginary;
};

Assume c1 is complex structure variable. c2 is pointer of complex type.

的一种方法是简单地一种结构分配给另一个。 C1 = C2 *
我试了一下。它工作正常。但是C2会给结构复杂的第一个元素的地址。那么如何在整个结构将被复制?难道我的指针更糊涂了?

One way is simply assign one structure to another. c1=*c2. I tried it. It works fine. But c2 will give the address of first element of structure complex. Then how the entire structure will be copied? Am i more confused with pointers?

另一种方法是使用的memcpy 的memcpy(安培; C1,C2的sizeof(结构复杂))

Another way is using memcpy memcpy(&c1,c2,sizeof(struct complex)).

我的疑问是成员明智的结构复制。
      的memcpy(安培; c1.real,&安培; C2->实,的sizeof(结构复杂))

My doubt is on member-wise structure copying. memcpy(&c1.real, &c2->real, sizeof(struct complex))

我读了上面的方法并不安全。因为如果我们改变结构成员的顺序,它不会工作。没有人解释了是不是安全?它混淆了我,因为两个变量都是相同的结构,我指定我要复制[真正在这种情况下。那么,为什么它是不是安全?可能有人请解释一下吗?

I read that the above method is not safe. Because it won't work if we change the order of structure members. Nobody explained how is not safe? It confuses me because both variables are same structures , i am specifying what i want to copy [real in this case]. Then why is it not safe? could anybody please explain it?

推荐答案

当然,唯一理智的方法是使用赋值:

Of course the only sane way is to use assignment:

c1 = *c2;

这是真的, C2 的实际数值,一个指针的结构中,是在与第一部件的地址。但that't 不要紧。参与分配的值的类型的都是结构复杂的,也就是分配正在与所有需要的比特将被复制。

It's true that the actual numerical value of c2, a pointer to a structure, is the same as the address of the first member. But that't doesn't matter. The types of the values involved in the assignment are both struct complex, that is what the assignment is working with and all needed bits will be copied.

待办事项的不可以使用的memcpy()复制结构,也绝对没有问题!它其实可以净负,因为次结构可以包含填充它赋值运算符(作为一个运营商,其code生成是由编译器为这个特定的使用控制)可以处理并跳过,节省了时间。功能的memcpy()并没有当然的填充想法,它的工作是复制所有字节,这就是它会做的。

Do not use memcpy() to copy a structure, there is absolutely no point! It can in fact be a net negative, since th structure can contain padding which the assignment operator (being an operator, whose code generation is controlled by the compiler for this specific usage) can deal with and skip over, saving time. The function memcpy() has no idea of the padding of course, it's job is to copy all bytes and that's what it will do.

另外,当然,这样做的的使用的memcpy()来复制各个 INT -sized成员。这只是......不对,不这样做。

Also, of course, do not use memcpy() to copy individual int-sized members. That's just ... not right, don't do that.

另外,分配是较高的水平,从而更抽象的,并因此更紧凑和更有效地进行通信。

Also, the assignment is higher-level, thus more abstract, and thus more compact and communicates more efficiently.

当然,如果结构包含其自身的指针(你没有),你必须小心做的深拷贝和分配的指向的数据和复制,太多新的实例。这已无关,与如何实际复制,虽然完成的。

Of course, if the structure contains pointers of its own (yours does not) you must take care to do a deep copy, and allocate new instances of the pointed-to data and copying that too. That has nothing to do with how the actual copying is done though.

使用结构分配。始终。

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

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