Deep copy and shadow copy

本文详细解释了深拷贝和浅拷贝的概念及其在对象复制中的应用。通过具体实例展示了两种拷贝方式的区别:浅拷贝仅复制对象引用而非实际内容;而深拷贝则完全复制所有直接和间接引用的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When copying a class instance to another,we think about the deep copy and shadow copy. If just assiging tha value to each class data member and ignoring resource reallocation (if there are resoures, such heap, file handle and so on), this is called as shadow copy. The two objects have share same resources. If reallocating resource, it is deep copy. In this case, the two objects have own resource.

 

The next is from mircrosoft document:
A shallow copy creates a new instance of the same type as the original object, and then copies the non-static fields of the original object. If the field is a value type, a bit-by-bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not; therefore, the reference in the original object and the reference in the clone point to the same object. In contrast, a deep copy of an object duplicates everything directly or indirectly referenced by the fields in the object.


Examples

//Shadow copy

Class CopyIns {
  private:
    char * cipher;
    int len;

  public:
    CopyIns(int len=10){
      if(len <=0 || len > 128){
        len = 10;
      }
     
      cipher = new char[len*sizeof(char)];
      if(NULL == cipher){
        len = 0;
      }
    }

    CopyIns(const CopyIns & rth){
      delete [] this->cipher;
      this->cipher = NULL;
      this->len = 0;

      this->cipher = rth.cipher;
      this->len = rth.len;
    }
 
    CopyIns & operator=(const CopyIns & rth){
      delete [] this->cipher;
      this->cipher = NULL;
      this->len = 0;
  
      if(rth.len > 0 && rth.cipher != NULL){
        this->cipher = new char[rth.len];
        if(this->cipher != NULL) {
          strncpy(this->cipher,rth.cipher,rth.len);
          this->len = rth.len;
          this->cipher[rth.len-1] = 0;
        }
      }
    }
}


main(){
  CopyIns a(5);
  CopyIns b=a;
}

the a's ciper and b's ciper refer to same heap memory. This is shadow copy. But it is dangerous

that two objects point to same heap memory.


//Deep copy
Class CopyIns {
  private:
    char * cipher;
    int len;

  public:
    CopyIns(int len=10){
      if(len <=0 || len > 128){
        len = 10;
      }
     
      cipher = new char[len*sizeof(char)];
      if(NULL == cipher){
        len = 0;
      }
    }

    CopyIns(const CopyIns & rth){
      delete [] this->cipher;
      this->cipher = NULL;
      this->len = 0;

      if(rth.len > 0 && rth.cipher != NULL){
        this->cipher = new char(rth.len);
        if(this->cipher != NULL){
          strncpy(this->cipher,rth.cipher,rth.len);
          this->len = rth.len;
          this->cipher[rth.len-1] = 0;
        }
      }
    }
 
    CopyIns & operator=(const CopyIns & rth){
      delete [] this->cipher;
      this->cipher = NULL;
      this->len = 0;
  
      if(rth.len > 0 && rth.cipher != NULL){
        this->cipher = new char(rth.len);
        if(this->cipher != NULL){
          strncpy(this->cipher,rth.cipher,rth.len);
          this->len = rth.len;
          this->cipher[rth.len-1] = 0;
        }
      }
    }
}


main(){
  CopyIns a(5);
  CopyIns b=a;
}

the a's ciper and b's ciper do not refer to same heap memory. This is deep copy.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值