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


当前回答

{想象两个对象:相同类型_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.

其他回答

浅拷贝:将成员值从一个对象复制到另一个对象。

深度复制:将成员值从一个对象复制到另一个对象。                       任何指针对象复制和复制。

例子:

class String
{
     int   size;
     char* data;
};

String  s1("Ace");   // s1.size = 3 s1.data=0x0000F000

String  s2 = shallowCopy(s1);
 // s2.size =3 s2.data = 0X0000F000
String  s3 = deepCopy(s1);
 // s3.size =3 s3.data = 0x0000F00F
 //                      (With Ace copied to this location.)

什么是浅复制?

浅复制是对象的逐位复制。创建一个新对象,该对象具有原始对象中值的精确副本。如果对象的任何字段是对其他对象的引用,则只复制引用地址,即只复制内存地址。

In this figure, the MainObject1 has fields field1 of type int, and ContainObject1 of type ContainObject. When you do a shallow copy of MainObject1, MainObject2 is created with field2 containing the copied value of field1 and still pointing to ContainObject1 itself. Note that since field1 is of primitive type, its value is copied to field2 but since ContainedObject1 is an object, MainObject2 still points to ContainObject1. So any changes made to ContainObject1 in MainObject1 will be reflected in MainObject2.

如果这是浅复制,我们看看什么是深复制?

什么是深度复制?

深度复制复制所有字段,并复制由字段指向的动态分配的内存。当对象与其引用的对象一起复制时,就会发生深度复制。

在这个图中,MainObject1具有类型为int的字段field1和类型为ContainObject的字段ContainObject1。在对MainObject1进行深度复制时,MainObject2创建时,field2包含从field1复制的值,而ContainObject2包含从ContainObject1复制的值。注意,MainObject1中对ContainObject1所做的任何更改都不会反映在MainObject2中。

篇好文章

struct sample
{
    char * ptr;
}
void shallowcpy(sample & dest, sample & src)
{
    dest.ptr=src.ptr;
}
void deepcpy(sample & dest, sample & src)
{
    dest.ptr=malloc(strlen(src.ptr)+1);
    memcpy(dest.ptr,src.ptr);
}

在面向对象编程中,类型包括成员字段的集合。这些字段可以按值或引用(即指向值的指针)存储。

在浅复制中,将创建该类型的新实例,并将值复制到新实例中。引用指针也像值一样被复制。因此,引用指向原始对象。对引用存储的成员的任何更改都同时出现在原始和副本中,因为没有对引用的对象进行复制。

在深度复制中,像以前一样复制按值存储的字段,但不复制按引用存储的对象指针。相反,对引用的对象进行深度复制,并存储指向新对象的指针。对这些引用对象所做的任何更改都不会影响该对象的其他副本。

“ShallowCopy”指向与“Source”相同的内存位置。“DeepCopy”指向内存中的不同位置,但内容是相同的。