写时拷贝
有时候存在一种拷贝一个对象, 但是只是对对象成员进行读操作,如果进行深拷贝会造成资源的浪费
写时拷贝就可以解决这个问题,在拷贝对象是用浅拷贝,只有对对象进行写操作时才会分配空间

写时拷贝通过引用计数进行实现
引用计数两种实现方案
第一种方案 是通过在开空间的同时开一小块空间存放引用计数如图:

class String
{
public:
String(const char * str = "")
:_size(strlen(str))
, _capcity(_size)
, _refcount(new int(1))
{
_str = new char[_capcity + 1];
strcpy(_str, str);
}
String(const String & s)
:_str(s._str)
, _size(s._size)
, _capcity(s._capcity)
, _refcount(s._refcount)
{
++(*_refcount);
}
void Swap(String & s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capcity, s._capcity);
swap(_refcount, s._refcount);
}
String & operator=(const String & s)
{
if (this != &s)
{
String temp(s); //创造一个临时对象因为其存在栈上即在当前函数栈针释放前会调用其构造函数数
Swap(temp); //故将当前对象的之前引用的对象交给temp释放,其成为新的对象
}
return *this;
}
//写时拷贝
void copyOnWrite()
{
if (*_refcount > 1)//当引用计数为1时不需要重新开空间
{
--(*_refcount);//重新开空间前之前的对象引用计数会减1
char * prev = _str;
_str = new char[_capcity + 1];//此时为当前对象开空间并实现深拷贝
_refcount = new int(1); //同时设置新的引用计数
strcpy(_str, prev);
}
}
~String()
{
if (--(*_refcount) == 0) //每次调用析构函数引用计数减1
//当引用计数减为0 时才会真正的释放空间
{
printf("%s\n", _str);
cout << "析构" << endl;
delete[] _str;
delete _refcount;
_size = _capcity = 0;
}
}
protected:
char * _str;
size_t _size;
size_t _capcity;
int * _refcount;
};
第二种方案 是类似operator new[] 函数 通过在开空间的同时在开的空间前面多开四个字节存放引用计数:

class String
{
public:
String(const char * str = "")
:_size(strlen(str))
, _capcity(_size)
{
_str = new char[_capcity + 5];
*(int*)_str = 1;
_str += 4;
strcpy(_str, str);
}
String(const String & s)
:_str(s._str)
, _size(s._size)
, _capcity(s._capcity)
{
++(*(int *)(_str - 4));
}
void Swap(String & s)
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capcity, s._capcity);
}
String & operator=(const String & s)
{
if (this != &s)
{
String temp(s);
Swap(temp);
}
return *this;
}
void copyOnWrite()
{
if (*(int *)(_str - 4) > 1)
{
--(*(int *)(_str - 4));
char * prev = _str;
_str = new char[_capcity + 5];
*(int*)_str = 1;
_str += 4;
strcpy(_str, prev);
}
}
~String()
{
if (--(*(int *)(_str - 4)) == 0) //每次调用析构函数引用计数减1
//当引用计数减为0 时才会真正的释放空间
{
printf("%s\n", _str);
cout << "析构" << endl;
delete[] (_str - 4);
_size = _capcity = 0;
}
}
protected:
char * _str;
size_t _size;
size_t _capcity;
};
2556

被折叠的 条评论
为什么被折叠?



