The shallow copy and deep copy of the string reference counter in C + +

Source: Internet
Author: User

Implementation of the string class under Linux operation--reference counter

1. Quote Counter

A person prefers to call him a double-pointer method, because he creates two pointers in the class, one is the pointer _str, and the other is the pointer _prefcount that is used to hold the number of the same space.

Class string{public:    string (char* str =  "")          :_STR (New char[strlen (str)  + 1])          , _prefcount (New int (1))     {         strcpy (_STR, STR);     }    string (String& s)         :_str (S._STR)           _prefcount (S._prefcount)     {        ++ (*_ Prefcount);     }    string& operator= (const String&  s)     {        /*char* tmp =  New char[strlen (S._STR)  + 1];        strcpy (tmp, s . _str);        delete[] _str;        _str  = tmp;*/ //this notation does not conform to the principle of reference counter         /*swap (_str, s. _STR);*/        if  (--(*_prefcount)  == 0)          {             Delete[] _str;            delete[] _ Prefcount;        }        _ str = s._str;        _prefcount = s._prefcount;         ++ (*_prefcount);         return *this;    }    ~string ()     {         if  (--(*_prefcount)  == 0)         {             delete[] _str;             delete[] _pRefCount;         }    }private:    char* _str;    After the  /*static int _refcount;*///becomes static, it needs to be defined outside the class     int* _prefcount;};/ /int string::_refcount = 0;void test1 () {    string s1 ("xxx");     STRING S2 (S1);     STRING S3 ("yyy");     S3 = S2;    /*STRING S4 (S3); */}int main () {     test1 ();     system ("pause");     return 0;}

This kind of writing is easy to understand, but also easy to think of, but because to open two pieces of space a large piece of small, easy to cause the system to produce a lot of memory fragmentation, so there is another way of writing

2. Reference counter notation Two

This way of thinking is to open up space when more than four bytes of space, in the head with four bytes of space to save the number of points to the same piece of space, so that the production of memory fragments to avoid, but also make it easier to operate, do not have to operate two pieces of space.

Class string{public:    string (char* str =  "")          :_STR (New char[strlen (str)  + 5])     {         * (int*) _str = 1;         _str += 4;        strcpy (_STR, STR);     }    string (string& s)         :_str (S._STR)     {        ++ (* (int*) (_str - 4 ));     }    string& operator= (Const String& s)     {        if  (_STR != S._STR)         {             releasE ();            _str = s._str;             ++getrefcount (_STR);         }        return *this;     }    ~string ()     {         if  (--(* (int*) (_str - 4))  == 0)          {            delete[] (_str - 4);         }    }    int&  Getrefcount (CHAR* STR)     {        return  * (int*) (str - 4);     }    void release ()      {        if  (--getrefcount (_STR)  == 0)          {            delete[] (_str  - 4);        }    }private:     char* _str;}; Int main () {    system ("pause");     return 0;}


The shallow copy and deep copy of the string reference counter in C + +

Related Article

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.