写时拷贝技术是通过"引用计数"实现的,在分配空间的时候多分配4个字节,用来记录有多少个指针指向块空间,当有新的指针指向这块空间时,引用计数加一,当要释放这块空间时,引用计数减一(假装释放),直到引用计数减为0时才真的释放掉这块空间。当有的指针要改变这块空间的值时,再为这个指针分配自己的空间(注意这时引用计数的变化,旧的空间的引用计数减一,新分配的空间引用计数加一)。

#include<iostream>
#include<new.h>
#include<string>
using namespace std; //1解决内存泄漏
//2编写赋值语句
//3写时拷贝
class String;
ostream& operator<<(ostream &out, const String &s); /////////////////////////////////////////////////////////////////////
class String_rep
{
friend class String;
friend ostream& operator<<(ostream &out, const String &s); private:
String_rep(const char *str = "") : use_count_()
{
if (str == NULL)
{
data = new char[];
data[] = '\0';
}
else
{
data = new char[strlen(str) + ];
strcpy(data, str);
}
}
String_rep(const String_rep &rep)
{
this->data = rep.data;
}
String_rep& operator=(const String_rep &rep)
{
this->data = rep.data;
}
~String_rep()
{
if (data != NULL)
{
delete[]data;
data = NULL;
}
}
public:
void increment()
{
++use_count_;
} void decrement()
{
//引用计数为0,释放共享内存
if (--use_count_ == )
delete this;
} private:
char *data;
int use_count_;
}; //////////////////////////////////////////////////////
class String
{
friend ostream& operator<<(ostream& out, const String &s); public:
String(const char *str = "") :rep(new String_rep(str))
{
rep->increment();
}
String(const String &s)
{
rep = s.rep;
rep->increment();
}
String& operator=(const String &s)
{
if (&s != this)
{
this->rep->decrement(); //原有共享内存中的引用计数减一
this->rep = s.rep;
this->rep->increment(); //现有引用计数加一
}
return *this;
}
~String()
{
//String析构一次,引用计数减一
rep->decrement();
} public:
void to_upper();
String& operator+=(const String &str); private:
String_rep *rep;
}; /////////////////////////////////////////////////////////////////////////
ostream& operator<<(ostream &out, const String &s)
{
out << s.rep->data;
return out;
} //创建新的共享内存原来共享内存中值一样,然后再修改
void String::to_upper()
{
String *newStr = new String(this->rep->data);
this->rep->decrement();
this->rep = newStr->rep;
this->rep->increment(); char *str = this->rep->data;
while (*str != '\0')
{
*str -= ;
++str;
}
delete newStr;
} String& String::operator+=(const String &str)
{
char *ch = new char[strlen(str.rep->data) + strlen(this->rep->data) + ];
strcpy(ch,this->rep->data);
strcat(ch, str.rep->data); this->rep->decrement();
String_rep *s = new String_rep(ch);
this->rep = s;
this->rep->increment(); return *this;
} int main()
{
String s("abc");
String s1;
s1 = s; //
String s2("xyz");
String s3(s);
s2.to_upper(); s3 += s2;
cout << s2 << endl;
cout << s3 << endl; return ;
}
 
 
 
 
 
 

写时拷贝COW(copy-on-write)的更多相关文章

  1. rust漫游 - 写时拷贝 Cow<'_, B>

    rust漫游 - 写时拷贝 Cow<'_, B> Cow 是一个写时复制功能的智能指针,在数据需要修改或者所有权发生变化时使用,多用于读多写少的场景. pub enum Cow<'a ...

  2. 写时拷贝(Copy On Write)方案详解

    本文旨在通过对 写时拷贝 的四个方案(Copy On Write)分析,让大家明白写时拷贝的实现及原理. 关于浅拷贝与深拷贝,我在之前的博客中已经阐述过了  浅拷贝容易出现指针悬挂的问题,深拷贝效率低 ...

  3. [转] Linux写时拷贝技术(copy-on-write)

    PS:http://blog.csdn.net/zxh821112/article/details/8969541 进程间是相互独立的,其实完全可以看成A.B两个进程各自有一份单独的liba.so和l ...

  4. Linux写时拷贝技术(copy-on-write)

    COW技术初窥: 在Linux程序中,fork()会产生一个和父进程完全相同的子进程,但子进程在此后多会exec系统调用,出于效率考虑,linux中引入了“写时复制“技术,也就是只有进程空间的各段的内 ...

  5. 【转】Linux写时拷贝技术(copy-on-write)

    http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html 源于网上资料 COW技术初窥: 在Linux程序中,fork()会 ...

  6. copy-on-write(写时拷贝技术)

    今天看<Unix环境高级编程>的fork函数与vfork函数时,看见一个copy-on-write的名词,貌似以前也经常听见别人说过这个,但也一直不明白这究竟是什么东西.所以就好好在网上了 ...

  7. Linux写时拷贝技术【转】

    本文转载自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html COW技术初窥: 在Linux程序中,fork()会产 ...

  8. String写时拷贝实现

    头文件部分 1 /* 版权信息:狼 文件名称:String.h 文件标识: 摘 要:对于上版本简易的String进行优化跟进. 改进 1.(将小块内存问题与大块分别对待)小内存块每个对象都有,当内存需 ...

  9. Rust写时复制Cow<T>

    写时复制(Copy on Write)技术是一种程序中的优化策略,多应用于读多写少的场景.主要思想是创建对象的时候不立即进行复制,而是先引用(借用)原有对象进行大量的读操作,只有进行到少量的写操作的时 ...

随机推荐

  1. 《js高级程序设计》6.1.1-6.1.3——数据属性、访问器属性

    数据属性:该属性包含了一个数据值的位置,它包含了4个描述行为的特性:1. [[Configurable]]:表示是否能通过delete删除属性从而重新定义属性,能否修改属性的特性,能否把属性修改为访问 ...

  2. Centos7环境下搭建Nginx+Lua+Redis进行数据存取

    1.安装依赖环境 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 2.安装LuaJIT cd /usr/loca ...

  3. 巨蟒python全栈开发-第11阶段 ansible_project6

    今日大纲: 1.计划任务前端页面 2.计划任务新增实现 3.计划任务编辑 4.项目详情 5.文件上传 6.replace模块介绍 1.计划任务前端页面 2.计划任务新增实现 3.计划任务编辑 4.项目 ...

  4. 关于使用JavaMail发送邮件

    import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import javax.activati ...

  5. Shell 基本运算符 1

    Shell 和其他编程语言一样,支持多种运算符,包括: 算术运算符 关系运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr ...

  6. JVM -XX: 参数列表

    功能开关: 参数 默认值或限制 说明 参数 默认值 功能 -XX:-AllowUserSignalHandlers 限于Linux和Solaris,默认不启用 允许为java进程安装信号处理器,信号处 ...

  7. HTML静态网页--表单验证和事件

    1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...

  8. 浏览器间CSS样式兼容问题

    1.display:table居中显示 在chrome和safari浏览器上兼容问题 2.滤镜 在chrome浏览器中能正常显示,在360浏览器中不能正常显示 3.省略号问题 对于一行显示,基本上对所 ...

  9. 最短路径Dijkstra算法和Floyd算法整理、

    转载自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html 最短路径—Dijkstra算法和Floyd算法 Dijks ...

  10. [C++] 检查随机内存溢出

    C++程序的随机内存溢出是非常难处理的,windows提供了一些工具来缓解这个问题. windows debuger提供的Global Flags可以设置"enable heap tail ...