Write-time copy of String class (Copy-on-write)

Source: Internet
Author: User

    1. What is the principle of copying (copy-on-write) when writing?

      Copy-on-write, is actually using a counter. When the first class is constructed, the constructor of the 1,string class with the counter initial value allocates memory from the heap based on the parameters passed in, and the counter automatically accumulates when the other class needs the memory. When there is a destructor, this counter is reduced by 1 until the counter is 1 o'clock, that is, the last class is destructor. At this point, the program will actually delete this chunk of memory allocated from the heap.


2. What happens when a string is copied (copy-on-write) when it is written?

When a memory share occurs in the string class and the content changes, the write-time copy (Copy-on-write) is triggered. For example, the String class [], =, + =, + operator.


3. Under what circumstances does the string class occur with memory sharing?

If a class needs data from another class, it can share the memory of the class being used.

When you use data from another class, there are two things:

(1) Construct yourself with another class; Call the copy constructor.

(2) Assign a value to another class; Call the overloaded assignment operator function.


4. The implementation of the copy only when writing:


#define _crt_secure_no_warnings

#include <iostream>


using namespace Std;

Class String

{

Public

String (char* str = "")

: _str (new Char[strlen (str) +1])

, _pcount (new int (1))

{

strcpy (_STR,STR);

}

~string ()

{

if (_str! = NULL)

{

if (--*_pcount = = 0)

{

Delete[] _STR;

Delete _pcount;

}

}

}

String (const string& STR)

{

_str = Str._str;

_pcount = Str._pcount;

+ + (*_pcount);

}

string& operator= (const string& STR)

{

if (This! = &STR)

{

if (--(*_pcount) = = 0)

{

Delete[] _STR;

Delete _pcount;

}

_str = Str._str;

_pcount = Str._pcount;

+ + (*_pcount);

}

return *this;

}

Private

char* _str;

int* _pcount;

};

int main ()

{

String str1 ("Hello");

String str2 (STR1);

String STR3;

STR3 = str1;

return 0;

}

Note: Str1,str2,str3 shares the same block of memory.


The string class is dynamically opening up space on the heap, and we are opening up space while opening up an integral space to store the counter. All classes that share the same memory use the same counter.


Realize:

#define _crt_secure_no_warnings

#include <iostream>


using namespace Std;


Class String

{

Public

String (char *str = "")

: _str (new Char[strlen (str) +5])

{

* (int *) _STR = 1;

_str + = 4;

strcpy (_STR,STR);

}

~string ()

{

if (_str! = NULL)

{

if (--* (int *) (_str-4) ==0)

{

Delete[] (_str-4);

}

}

}

String (string& str)//Copy Construction

{

_str = Str._str;

++* (int *) (_str-4);

}

string& operator= (String &str)

{

if (This! = &STR)

{

if (--* (int *) (_str-4) = = 0)

{

Delete[] (_str-4);

}

_str = Str._str;

++* (int *) (str._str-4);

}

return *this;

}

Private

Char *_str;

};

int main ()

{

String str1 ("Zhang");

String str2 (STR1);

Str1.operator [] (3);

/*string str2;

STR2 = str1;

String str3 ("CCCCC");

String STR4;

STR4 = str3;*/

STR3 = str1;

return 0;

}



Write-time copy of String class (Copy-on-write)

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.