您的位置:首页 > 运维架构

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:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: