深度复制和浅复制的区别是什么?
当前回答
{想象两个对象:相同类型_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.
其他回答
我在这里没有看到一个简短的,容易理解的答案——所以我会试一试。
使用浅拷贝,源指向的任何对象也会被目标指向(因此不会复制引用的对象)。
使用深度复制,源指向的任何对象都将被复制,而目标指向的副本(因此现在每个引用的对象都有2个)。这是递归向下的对象树。
试着考虑下面的图像
例如Object。memberwisclone创建一个浅复制链接
并使用ICloneable接口,你可以得到深度拷贝,如这里所述
浅复制是创建一个新对象,然后将当前对象的非静态字段复制到新对象。如果字段是值类型——>,则执行字段的逐位复制;对于引用类型——>,引用被复制,但被引用的对象没有;因此,原始对象及其克隆对象引用同一个对象。
深度复制是创建一个新对象,然后将当前对象的非静态字段复制到新对象。如果字段是值类型——>,则执行字段的逐位复制。如果字段是引用类型——>,则执行引用对象的新副本。要克隆的类必须标记为[Serializable]。
简单来说,浅拷贝类似于按引用调用,深拷贝类似于按值调用
在引用调用中,函数的形式参数和实际参数都指向相同的内存位置和值。
在按值调用中,函数的形式形参和实际形参都是指不同的内存位置,但具有相同的值。
char * Source = "Hello, world.";
char * ShallowCopy = Source;
char * DeepCopy = new char(strlen(Source)+1);
strcpy(DeepCopy,Source);
“ShallowCopy”指向与“Source”相同的内存位置。 “DeepCopy”指向内存中的不同位置,但内容是相同的。
推荐文章
- 在支持循环和函数的语言中,是否存在“goto”的合法用例?
- 为什么不使用异常作为常规的控制流呢?
- 什么是序列化?
- 如何从表中导出所有数据到可插入的sql格式?
- 我如何复制一个哈希在Ruby?
- 每个递归都可以转换成迭代吗?
- 什么是ORM,它是如何工作的,我应该如何使用它?
- 我能在服务器端应用程序(PHP、Ruby、Python等)上读取URL的哈希部分吗?
- 多少个参数是太多?
- Git复制文件保存历史
- 对于不可变集合上的非突变“add”方法,最好的名称是什么?
- foo到底是什么意思?
- MySQL:在同一个MySQL实例上克隆MySQL数据库
- foreach和map有区别吗?
- 设计模式:工厂vs工厂方法vs抽象工厂