写时拷贝COW(copy-on-write)
#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)的更多相关文章
- rust漫游 - 写时拷贝 Cow<'_, B>
rust漫游 - 写时拷贝 Cow<'_, B> Cow 是一个写时复制功能的智能指针,在数据需要修改或者所有权发生变化时使用,多用于读多写少的场景. pub enum Cow<'a ...
- 写时拷贝(Copy On Write)方案详解
本文旨在通过对 写时拷贝 的四个方案(Copy On Write)分析,让大家明白写时拷贝的实现及原理. 关于浅拷贝与深拷贝,我在之前的博客中已经阐述过了 浅拷贝容易出现指针悬挂的问题,深拷贝效率低 ...
- [转] Linux写时拷贝技术(copy-on-write)
PS:http://blog.csdn.net/zxh821112/article/details/8969541 进程间是相互独立的,其实完全可以看成A.B两个进程各自有一份单独的liba.so和l ...
- Linux写时拷贝技术(copy-on-write)
COW技术初窥: 在Linux程序中,fork()会产生一个和父进程完全相同的子进程,但子进程在此后多会exec系统调用,出于效率考虑,linux中引入了“写时复制“技术,也就是只有进程空间的各段的内 ...
- 【转】Linux写时拷贝技术(copy-on-write)
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html 源于网上资料 COW技术初窥: 在Linux程序中,fork()会 ...
- copy-on-write(写时拷贝技术)
今天看<Unix环境高级编程>的fork函数与vfork函数时,看见一个copy-on-write的名词,貌似以前也经常听见别人说过这个,但也一直不明白这究竟是什么东西.所以就好好在网上了 ...
- Linux写时拷贝技术【转】
本文转载自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html COW技术初窥: 在Linux程序中,fork()会产 ...
- String写时拷贝实现
头文件部分 1 /* 版权信息:狼 文件名称:String.h 文件标识: 摘 要:对于上版本简易的String进行优化跟进. 改进 1.(将小块内存问题与大块分别对待)小内存块每个对象都有,当内存需 ...
- Rust写时复制Cow<T>
写时复制(Copy on Write)技术是一种程序中的优化策略,多应用于读多写少的场景.主要思想是创建对象的时候不立即进行复制,而是先引用(借用)原有对象进行大量的读操作,只有进行到少量的写操作的时 ...
随机推荐
- KiCad EDA 5.1.4 发布了
KiCad EDA 5.1.4 发布了 KiCad EDA 自豪地宣布 KiCad 5 系列最新稳定版发布.5.1.4 稳定版修复了来自 5.1.2 和 5.1.3 版本的关键错误修复和其他一些小改进 ...
- 使用 VSCODE 在 Windows 10 WSL 中远程开发
使用 VSCODE 在 Windows 10 WSL 中远程开发 安装 VSCODE 1.35+ 版本. 在 VSCODE 中安装 WSL 插件. 点击左下角的 WSL 图标. 打开项目,提示路径. ...
- UIView 判断是否visible
if(self.view == [(MyAppDelegate *)[[UIApplication sharedApplication] delegate].window.subviews objec ...
- Cython保护Python代码
注:.pyc也有一定的保护性,容易被反编译出源码... 项目发布时,为防止源码泄露,需要对源码进行一定的保护机制,本文使用Cython将.py文件转为.so进行保护.这一方法,虽仍能被反编译,但难度会 ...
- nodeJs学习-05 案例:http/fs/querystring/url
const http = require('http'); const fs = require('fs'); const querystring = require('querystring'); ...
- GIT 公钥配置
1.下载git 2.ssh-keygen -t rsa -C "xxx@xxx.com" 3.cd ~/.ssh 4.ls 5.cat id_rsa.pub 或者C:\User\x ...
- [HLSL]HLSL 入门参考 (dx11龙书附录B译文)
原文:[HLSL]HLSL 入门参考 (dx11龙书附录B译文) HLSL 高级着色语言 参考文档 龙书DirectX12现已推出中文版,其附录B的高级着色器语言参考的翻译质量比本文更高,有条件的读者 ...
- @bzoj - 4356@ Ceoi2014 Wall
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给出一个N*M的网格图,有一些方格里面存在城市,其中首都位于网格 ...
- Oracle基础学习4--Oracle权限传递
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/wang379275614/article/details/32215325 以下将用一个实例来解说: ...
- 【HAOI2015】树上染色
[HAOI2015]树上染色 这题思路好神仙啊,首先显然是树形dp,f[i][j]表示在以i为根的子树中选j个黑点对答案的贡献(并不是当前子树最大值),dp时只考虑i与儿子连边的贡献.此时(i,son ...