Deep copy and shadow copy

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.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章