zoukankan      html  css  js  c++  java
  • String 类 Copy-On-Write 技术以及使用时存在的风险

    先来看一下string 面试时的简易写法(使用的是深拷贝):

    class String
    {
        String()
            :str(new char[1])
        {
            str[0] = '';
        }
    
        String(char* p, size_t size)
            :str(new char[size + 1])
        {
            strcpy(str, p);
        }
    
        String(String& Str)
            :str(new char[strlen(Str.str)+1])
        {
            strcpy(str, Str.str);
        }
    
        String& operator=(String& Str)
        {
            if (this != &Str)
            {
                String StrTmp = Str;
                swap(str, StrTmp.str);
            }
            return *this;
        }
    
        ~String()
        {
            delete[] str;
        }

    //深拷贝与浅拷贝

      当对string的对象不进行修改,也就是只读的时候,我们创建新对象时可以用新的string类中的char*来指向原先旧的string中字符串的起始位置,如果用深拷贝就存在内存浪费的问题,因为我们每构造出一个对象时都是重新开辟新的空间来存储字符串。所以呢 Copy_On_Write的思想被提出来了,Copy_On_Write 就是指在修改string时才开辟空间来保存修改后的string,而如果不修改呢,我们就用浅拷贝,直接用string中char*进行赋值,引用计数加一,析构时如果引用计数减为0,则释放保存字符串的空间。

    写实拷贝的模型一:

    class String
    {
        String()
        :_str(new char[1]), _count(new int(1))
        {
            _str[0] = '';
        }
    
        String(char* p, size_t size)
            :_str(new char[size + 1]), _count(new int(1))
        {
            strcpy(_str, p);
        }
    
        String(String& Str)
            :_str(Str._str), _count(Str._count)
        {
            strcpy(_str, Str._str);
            *_count++;
        }
    
        String& operator=(String& Str)
        {
            if (this != &Str)
            {
                if (--*_count == 0)
                    delete[] _str;
                _str = Str._str;
                _count = Str._count;
                *_count++;
            }
            return *this;
        }
    
        ~String()
        {
            delete[] _str;
        }
    
    protected:
        char* _str;
        int* _count;   //引用计数
    };

     写实拷贝的模型二:

    class String
    {
    public:
        String()
        :_str(new char[5])
        {
            _str = _str + 4;
            _str[0] = '';
            int count = _Count(_str);
            count = 1;
        }
    
        String(char* p)
            :_str(new char[strlen(p) + 5])
        {
            _str = _str + 4;
            strcpy(_str, p);
            int& count = _Count(_str);
            count = 1;
        }
    
        String(String& Str)
            :_str(Str._str)
        {
            ++_Count(_str);
        }
    
        String& operator=(String& Str)
        {
            if (this != &Str)
            {
                if (_Count(_str))
                    delete[](_str - 4);
                _str = Str._str;
                ++_Count(_str);
            }
            return *this;
        }
    
        ~String()
        {
            if (--_Count(_str) == 0)
            delete[] _str;
        }
    
        int& _Count(char* p)
        {
            return *(int*)(p - 4);
        }
    protected:
        char* _str;
    };

    写实拷贝引发的问题:参见博文  

    C++ 之 stl::string 写时拷贝导致的问题

  • 相关阅读:
    c++局部变量在外可用的方法
    Git更新代码到本地
    Redis客户端断开重连功能要点
    android make
    cb-A10 调整分区
    linux内核学习:中断中推后执行的部分
    linux内核学习:中断
    linux内核学习:进程调度
    linux内核学习:进程管理
    ubuntu 安装android源码编译环境 遇到的问题
  • 原文地址:https://www.cnblogs.com/shihaochangeworld/p/5514339.html
Copyright © 2011-2022 走看看