深度复制和浅复制的区别是什么?
当前回答
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);
}
其他回答
简而言之,这取决于什么指向什么。在浅拷贝中,对象B指向对象a在内存中的位置。在深度复制中,对象A的内存位置中的所有东西都被复制到对象B的内存位置。
这篇wiki文章有一个很好的图表。
http://en.wikipedia.org/wiki/Object_copy
浅拷贝:将成员值从一个对象复制到另一个对象。
深度复制:将成员值从一个对象复制到另一个对象。 任何指针对象复制和复制。
例子:
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.)
{想象两个对象:相同类型_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.
浅克隆: 定义:“对象的浅拷贝复制‘主’对象,但不复制内部对象。” 当一个自定义对象(例如。雇员)只有原始的,字符串类型的变量,然后你使用浅克隆。
Employee e = new Employee(2, "john cena");
Employee e2=e.clone();
返回super.clone();在重写的clone()方法中,您的工作就结束了。
深克隆: 定义:“与浅拷贝不同,深拷贝是对象的完全独立副本。” 表示当一个Employee对象持有另一个自定义对象时:
Employee e = new Employee(2, "john cena", new Address(12, "West Newbury", "Massachusetts");
然后,您必须在覆写的clone()方法中编写代码来克隆'Address'对象。否则Address对象不会被克隆,当你在克隆的Employee对象中改变Address的值时,它会导致一个错误,这也反映了原始的一个。