deep copy vs shallow copy

  1. 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.
  2. Coming from C++ POV, there would be one scenario that i will use shallow copy : when implementing copy-on-write mechanism.
  3. Flyweight pattern would be an acceptable use of a shallow copy. Lazy copying is also a shallow copy.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. Shallow:

    Before Copy Shallow Copying Shallow Done

    Deep:

    Before Copy Deep Copying Deep Done


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值