Deep copy problem

Source: Internet
Author: User

When a pointer object is inside a class, there is a crash problem because a shallow copy is pointing to the same piece of memory by two objects! To solve this, a deep copy, write-time copy is introduced.

1. Shallow copy (new open a pointer to do reference counting)

Shallow copy + reference count (pointer) class string{public:string (char* str= "")//default when given \0:_str (new Char[strlen (str) +1]), _pcount (new int (1)) { strcpy (_STR,STR); *_pcount=1;} String (string & S): _str (S._STR), _pcount (S._pcount) {(*_pcount) + +;} string& operator= (const string& s) {if (this!=&s) {this->_release (); _str=s._str;_pcount=s._pcount; (*_ Pcount) + +;} return *this;} ~string () {_release ();} void Print () {printf ("%s%d\n", _str,*_pcount);} protected:char* _str;int* _pcount;void _release () {if (--(*_pcount) ==0) {Delete [] _str;delete _pcount;}}; int main () {String s ("Hello"); S.print (); String S1 (s); S1. Print (); String S2=s;s2. Print (); System ("pause"); return 0;}

2. Shallow copy (leave int data size deposit counter in original string)

Shallow copy + reference count (_str before giving an int data size deposit counter) class string{public:string (char* str= ""): _str (new Char[strlen (str) +5]) {_str+=4; strcpy (_STR,STR); _getcount (_STR) = 1;} String (const string& s)//:_str (new Char[strlen (S._STR) +5])//Copy construction is not a construction, why not open space. Shallow copy points to the same piece of space without opening//: _str (S._STR) {_str+=4;_str=s._str;_getcount (_STR) +=1;} string& operator= (const string& s) {if (this!=&s) {this->_release (); _str+=4;_str=s._str;_getcount (_ STR) +=1;} return *this;} ~string () {_release ();} void Print () {printf ("%s%d\n", _str,_getcount (_STR));} protected:char* _str;int count;void _release () {if (--_getcount (_STR) ==0) {delete [] (_str-4);}} int& _getcount (char* str) {return * ((int*) str-1);}; int main () {String s ("Hello"); S.print (); String S1 (s); S1. Print (); String S2=s;s2. Print (); System ("pause"); return 0;}

3. Deep copy

Deep Copy Class string{public:string (char* str= ""): _str (new Char[strlen (str) +1]) {strcpy (_STR,STR);} String (const string& s)//:_str (new Char[strlen (S._STR) +1])///This method has a wood error: _str (NULL) {//strcpy (_STR,S._STR); String tmp (S._STR); swap (_STR,TMP._STR);} string& operator= (const string& s) {//strcpy (_STR,S._STR); why not. 2 pointers point to the same piece of space. 1) Release 2 times. 2) One move, two changes. 3) _str original memory does not release string tmp (S._STR); swap (_STR,TMP._STR); return *this;} ~string () {delete [] _str;} void Print () {printf ("%s\n", _str);} Protected:char* _str;}; int main () {String s ("Hello"); S.print (); String S1 (s); S1. Print (); String S2=s;s2. Print (); System ("pause"); return 0;}

Use the figure to analyze: 650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/F3/wKioL1bzp96QYF5CAAB6iRpf6wo573.png "title = "C++.png" alt= "Wkiol1bzp96qyf5caab6irpf6wo573.png"/>

This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1754867

Deep copy problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.