写时复制Copy-On-Write

Copy-on-Write简称COW,不是奶牛,好处就是能保证数据的完整性,掉电的话容易恢复。
在执行复制和赋值的时候,不会真正的Copy,只有真正对内容进行修改时,才会进行复制,复制完之后再去修改-->懒复制
•Copy-On-Write一定使用了“引用计数”,必然有一个变量类似于RefCnt
•当第一个string对象str1构造时,string的构造函数会根据传入的参数从堆上分配内存
•当有其它string对象复制str1时,这个RefCnt会自动加1
•当有对象析构时,这个计数会减1;直到最后一个对象析构时,RefCnt为0,此时,程序才会真正的释放这块从堆上分配的内存



•在共享同一块内存的类发生 内容改变时,才会发生Copy-On-Write
•比如string类的 []、=、+=、+、操作符赋值,还有一些string类中诸如insert、replace、append等成员函数
#include <stdio.h>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;


//std::string并不是线程安全的

int main(void)
{
	string s1 = "hello,world";
	string s2 = s1;
	string s3 = s2;
	const char * p1 = s1.c_str();
	const char * p2 = s2.c_str();
	const char * p3 = s3.c_str();

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	printf("p1 = %p\n", p1);
	printf("p2 = %p\n", p2);
	printf("p3 = %p\n", p3);

	s3[0] = 'X';
	cout << "发生修改之后:" << endl;
	p1 = s1.c_str();
	p2 = s2.c_str();
	p3 = s3.c_str();

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	printf("p1 = %p\n", p1);
	printf("p2 = %p\n", p2);
	printf("p3 = %p\n", p3);
        return 0;
}

执行后的结果为:
s1 = hello,world
s2 = hello,world
s3 = hello,world
p1 = 0x204f028
p2 = 0x204f028
p3 = 0x204f028
发生修改之后:
s1 = hello,world
s2 = hello,world
s3 = Xello,world
p1 = 0x204f028
p2 = 0x204f028
p3 = 0x204f058

显而易见,当修改其内容时,发生Copy-On-Write

#include <stdio.h>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;


//std::string并不是线程安全的

int main(void)
{
	string s1 = "hello,world";
	string s2 = s1;
	string s3 = s2;
	const char * p1 = s1.c_str();
	const char * p2 = s2.c_str();
	const char * p3 = s3.c_str();

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	printf("p1 = %p\n", p1);
	printf("p2 = %p\n", p2);
	printf("p3 = %p\n", p3);

	s3[0] = 'X';
	cout << "发生修改之后:" << endl;
	p1 = s1.c_str();
	p2 = s2.c_str();
	p3 = s3.c_str();

	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	printf("p1 = %p\n", p1);
	printf("p2 = %p\n", p2);
	printf("p3 = %p\n", p3);

	cout << endl;
	cout << "读取s2[0]之后" << endl;
	cout << s2[0] << endl;
	p1 = s1.c_str();
	p2 = s2.c_str();
	p3 = s3.c_str();
	cout << "s1 = " << s1 << endl;
	cout << "s2 = " << s2 << endl;
	cout << "s3 = " << s3 << endl;
	printf("p1 = %p\n", p1);
	printf("p2 = %p\n", p2);
	printf("p3 = %p\n", p3);

	return 0;
}


执行结果为:
s1 = hello,world
s2 = hello,world
s3 = hello,world
p1 = 0x211e028
p2 = 0x211e028
p3 = 0x211e028
发生修改之后:
s1 = hello,world
s2 = hello,world
s3 = Xello,world
p1 = 0x211e028
p2 = 0x211e028
p3 = 0x211e058

读取s2[0]之后
h
s1 = hello,world
s2 = hello,world
s3 = Xello,world
p1 = 0x211e028
p2 = 0x211e088
p3 = 0x211e058



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值