写时拷贝

写时拷贝

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

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

在这里插入图片描述

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;
};
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值