deep copy vs shallow copy
2012-09-14 18:27
555 查看
When the owned variables are immutable types, a shallow copy is sufficient. A deep copy is possible, but would only result in additional memory
use.
Coming from C++ POV, there would be
one scenario that i will use shallow copy : when implementing copy-on-write mechanism.
Flyweight
pattern would be an acceptable use of a shallow copy. Lazy copying is also a shallow copy.
Shallow
copies are quicker to make, which is often desirable for obvious reasons. The ultimate in shallow copying is obviously just a copy of a reference to the original object. If you're not mutating an object and can guarantee it won't get changed in other ways
while you work with it then the more shallow the copy is, the better.
Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow
copy, two collections now share the individual elements.Deep copies duplicate everything. A deep copy of a collection is two collections with
all of the elements in the original collection duplicated.
ShallowCopy points to the same location in memory as 'Source' does. 'DeepCopy' points to a different location in memory, but the contents are the
same.
In Simple Terms, a Shallow Copy is similar to Call By Reference and a Deep Copy is similar to Call By Value In
Call By Reference, Both formal and actual parameters of a function refers to same memory location and the value.In Call By Value, Both formal
and actual parameters of a functions refers to different memory location but having the same value.
Shallow:
Deep:
use.
Coming from C++ POV, there would be
one scenario that i will use shallow copy : when implementing copy-on-write mechanism.
Flyweight
pattern would be an acceptable use of a shallow copy. Lazy copying is also a shallow copy.
Shallow
copies are quicker to make, which is often desirable for obvious reasons. The ultimate in shallow copying is obviously just a copy of a reference to the original object. If you're not mutating an object and can guarantee it won't get changed in other ways
while you work with it then the more shallow the copy is, the better.
Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow
copy, two collections now share the individual elements.Deep copies duplicate everything. A deep copy of a collection is two collections with
all of the elements in the original collection duplicated.
ShallowCopy points to the same location in memory as 'Source' does. 'DeepCopy' points to a different location in memory, but the contents are the
same.
In Simple Terms, a Shallow Copy is similar to Call By Reference and a Deep Copy is similar to Call By Value In
Call By Reference, Both formal and actual parameters of a function refers to same memory location and the value.In Call By Value, Both formal
and actual parameters of a functions refers to different memory location but having the same value.
Shallow:
Deep:
相关文章推荐
- Deep Copy VS Shallow Copy
- C++类中的4个特殊函数 && Object copy (Deep vs. Shallow vs. Lazy copy)
- 浅拷贝和深拷贝深入理解(shallow copy VS deep copy)
- shallow copy and deep copy (浅拷贝 vs 深拷贝)
- C# Tips-浅拷贝和深拷贝(shallow copy VS deep copy )
- Python Shallow Copy VS. Deep Copy (Python浅拷贝和深拷贝)
- C# Tips-浅拷贝和深拷贝(shallow copy VS deep copy )
- 浅拷贝和深拷贝(shallow copy VS deep copy )
- deep copy vs shallow copy
- Shallow vs. deep copy
- Summary: Deep Copy vs. Shallow Copy vs. Lazy Copy
- difference between Python shallow copy and deep copy
- Shallow Copy(影子拷贝) VS Deep Copy(深拷贝)
- python deep copy and shallow copy
- Java Clone, Shallow Copy and Deep Copy
- .NET深入学习笔记(4):深拷贝与浅拷贝(Deep Copy and Shallow Copy)
- .NET深入学习笔记(4):深拷贝与浅拷贝(Deep Copy and Shallow Copy)(转载)
- [转载]:Shallow Copy and Deep Copy
- Python: Shallow and deep copy operations
- shallow copy and deep copy