深度复制和浅复制的区别是什么?


当前回答

{想象两个对象:相同类型_t的A和B(相对于c++),你正在考虑将A浅/深复制到B}

浅拷贝: 简单地将a的引用复制到b中,把它看作是a的地址的副本。 因此,A和B的地址将是相同的,即它们将指向相同的内存位置,即数据内容。

Deep copy: Simply makes a copy of all the members of A, allocates memory in a different location for B and then assigns the copied members to B to achieve deep copy. In this way, if A becomes non-existant B is still valid in the memory. The correct term to use would be cloning, where you know that they both are totally the same, but yet different (i.e. stored as two different entities in the memory space). You can also provide your clone wrapper where you can decide via inclusion/exclusion list which properties to select during deep copy. This is quite a common practice when you create APIs.

只有当你了解其中的利害关系时,你才可以选择做浅拷贝。当你在c++或C中有大量的指针要处理时,做一个对象的浅拷贝真的是一个坏主意。

EXAMPLE_OF_DEEP COPY_ An example is, when you are trying to do image processing and object recognition you need to mask "Irrelevant and Repetitive Motion" out of your processing areas. If you are using image pointers, then you might have the specification to save those mask images. NOW... if you do a shallow copy of the image, when the pointer references are KILLED from the stack, you lost the reference and its copy i.e. there will be a runtime error of access violation at some point. In this case, what you need is a deep copy of your image by CLONING it. In this way you can retrieve the masks in case you need them in the future.

EXAMPLE_OF_SHALLOW_COPY I am not extremely knowledgeable compared to the users in StackOverflow so feel free to delete this part and put a good example if you can clarify. But I really think it is not a good idea to do shallow copy if you know that your program is gonna run for an infinite period of time i.e. continuous "push-pop" operation over the stack with function calls. If you are demonstrating something to an amateur or novice person (e.g. C/C++ tutorial stuff) then it is probably okay. But if you are running an application such as surveillance and detection system, or Sonar Tracking System, you are not supposed to keep shallow copying your objects around because it will kill your program sooner or later.

其他回答

浅复制是创建一个新对象,然后将当前对象的非静态字段复制到新对象。如果字段是值类型——>,则执行字段的逐位复制;对于引用类型——>,引用被复制,但被引用的对象没有;因此,原始对象及其克隆对象引用同一个对象。

深度复制是创建一个新对象,然后将当前对象的非静态字段复制到新对象。如果字段是值类型——>,则执行字段的逐位复制。如果字段是引用类型——>,则执行引用对象的新副本。要克隆的类必须标记为[Serializable]。

除了上述所有定义之外,还有一个最常用的深度复制,位于类的复制构造函数(或重载赋值操作符)中。

浅复制——>是当你没有提供复制构造函数时。在这里,只复制对象,但不复制类的所有成员。

深度复制——>是当您决定在类中实现复制构造函数或重载赋值,并允许复制类的所有成员时。

MyClass& MyClass(const MyClass& obj) // copy constructor for MyClass
{
          // write your code, to copy all the members and return the new object
}
MyClass& operator=(const MyClass& obj) // overloading assignment operator,
{
          // write your code, to copy all the members and return the new object
}

浅拷贝构造一个新的复合对象,并将其对原始对象的引用插入其中。

与浅拷贝不同,deepcopy构造新的复合对象,并插入原复合对象的原对象副本。

让我们举个例子。

import copy
x =[1,[2]]
y=copy.copy(x)
z= copy.deepcopy(x)
print(y is z)

上面的代码输出FALSE。

让我们看看怎么做。

原始复合对象x=[1,[2]](称为复合,因为它在对象中有对象(Inception))

如图所示,列表中有一个列表。

然后使用y = copy.copy(x)创建它的浅拷贝。python在这里所做的是,它将创建一个新的复合对象,但其中的对象指向原始对象。

在图像中,它为外层列表创建了一个新的副本。但内部列表与原始列表保持一致。

现在我们使用z = copy.deepcopy(x)创建它的深度复制。python在这里所做的是,它将为外部列表和内部列表创建新对象。如下图所示(红色高亮部分)。

最后代码输出False,因为y和z不是相同的对象。

HTH.

简而言之,这取决于什么指向什么。在浅拷贝中,对象B指向对象a在内存中的位置。在深度复制中,对象A的内存位置中的所有东西都被复制到对象B的内存位置。

这篇wiki文章有一个很好的图表。

http://en.wikipedia.org/wiki/Object_copy

尤其是iOS开发者:

如果B是a的浅拷贝,那么对于原语数据,它就像B = [a assign];对于对象,B = [A retain];

B和A指向同一个内存位置

如果B是a的深层副本,则B = [a copy];

B和A指向不同的内存位置

B的内存地址与A的相同

B和A的含量相同