`
feipigwang
  • 浏览: 744633 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

标准C++类string的Copy-On-Write技术(一)

阅读更多

标准C++类std::string的

内存共享和Copy-On-Write技术

陈皓

1、 概念

Scott Meyers在《More Effective C++》中举了个例子,不知你是否还记得?在你还在上学的时候,你的父母要你不要看电视,而去复习功课,于是你把自己关在房间里,做出一副正在复习功课的样子,其实你在干着别的诸如给班上的某位女生写情书之类的事,而一旦你的父母出来在你房间要检查你是否在复习时,你才真正捡起课本看书。这就是“拖延战术”,直到你非要做的时候才去做。

当然,这种事情在现实生活中时往往会出事,但其在编程世界中摇身一变,就成为了最有用的技术,正如C++中的可以随处声明变量的特点一样,Scott Meyers推荐我们,在真正需要一个存储空间时才去声明变量(分配内存),这样会得到程序在运行时最小的内存花销。执行到那才会去做分配内存这种比较耗时的工作,这会给我们的程序在运行时有比较好的性能。必竟,20%的程序运行了80%的时间。

当然,拖延战术还并不只是这样一种类型,这种技术被我们广泛地应用着,特别是在操作系统当中,当一个程序运行结束时,操作系统并不会急着把其清除出内存,原因是有可能程序还会马上再运行一次(从磁盘把程序装入到内存是个很慢的过程),而只有当内存不够用了,才会把这些还驻留内存的程序清出。

写时才拷贝(Copy-On-Write)技术,就是编程界“懒惰行为”——拖延战术的产物。举个例子,比如我们有个程序要写文件,不断地根据网络传来的数据写,如果每一次fwrite或是fprintf都要进行一个磁盘的I/O操作的话,都简直就是性能上巨大的损失,因此通常的做法是,每次写文件操作都写在特定大小的一块内存中(磁盘缓存),只有当我们关闭文件时,才写到磁盘上(这就是为什么如果文件不关闭,所写的东西会丢失的原因)。更有甚者是文件关闭时都不写磁盘,而一直等到关机或是内存不够时才写磁盘,Unix就是这样一个系统,如果非正常退出,那么数据就会丢失,文件就会损坏。

呵呵,为了性能我们需要冒这样大的风险,还好我们的程序是不会忙得忘了还有一块数据需要写到磁盘上的,所以这种做法,还是很有必要的。

2、 标准C++std::stringCopy-On-Write

在我们经常使用的STL标准模板库中的string类,也是一个具有写时才拷贝技术的类。C++曾在性能问题上被广泛地质疑和指责过,为了提高性能,STL中的许多类都采用了Copy-On-Write技术。这种偷懒的行为的确使使用STL的程序有着比较高要性能。

这里,我想从C++类或是设计模式的角度为各位揭开Copy-On-Write技术在string中实现的面纱,以供各位在用C++进行类库设计时做一点参考。

在讲述这项技术之前,我想简单地说明一下string类内存分配的概念。通过常,string类中必有一个私有成员,其是一个char*,用户记录从堆上分配内存的地址,其在构造时分配内存,在析构时释放内存。因为是从堆上分配内存,所以string类在维护这块内存上是格外小心的,string类在返回这块内存地址时,只返回const char*,也就是只读的,如果你要写,你只能通过string提供的方法进行数据的改写。

2.1、 特性

由表及里,由感性到理性,我们先来看一看string类的Copy-On-Write的表面特征。让我们写下下面的一段程序:


#include<stdio.h></stdio.h>

#include <iostream></iostream>

using namespace std;

main()

{

string str1 = "hello world";

string str2 = str1;

printf ("Sharing the memory:\n");

printf ("\tstr1's address: %x\n", str1.c_str() );

printf ("\tstr2's address: %x\n", str2.c_str() );

str1[1]='q';

str2[1]='w';

printf ("After Copy-On-Write:\n");

printf ("\tstr1's address: %x\n", str1.c_str() );

printf ("\tstr2's address: %x\n", str2.c_str() );

return 0;

}

这个程序的意图就是让第二个string通过第一个string构造,然后打印出其存放数据的内存地址,然后分别修改str1str2的内容,再查一下其存放内存的地址。程序的输出是这样的(我在VC6.0g++ 2.95都得到了同样的结果):


> g++ -o stringTest stringTest.cpp

> ./stringTest

Sharing the memory:

str1's address: 343be9

str2's address: 343be9

After Copy-On-Write:

str1's address: 3407a9

str2's address: 343be9

从结果中我们可以看到,在开始的两个语句后,str1str2存放数据的地址是一样的,而在修改内容后,str1的地址发生了变化,而str2的地址还是原来的。从这个例子,我们可以看到string类的Copy-On-Write技术。

下一页->

(版权所有,转载时请注明作者和出处)

分享到:
评论

相关推荐

    标准C++类string的Copy-On-Write技术

    里,我想从C++类或是设计模式的角度为各位揭开Copy-On-Write技术在string中实现的面纱,以供各位在用C++进行类库设计时做一点参考

    STL 的string类怎么啦

    老实说,我几年前也有同样的痛苦(就是当我写下《标准C++类string的Copy-On-Write技术》之前的一段时间)。那时,我不得不研究那根本不是给人看的SGI出品的string类的源码,代码的可读性几乎为零,而且随着了解越...

    Turbo C++ 3.0[DISK]

    special offer, and write for full details on how to receive a free IntroPak containing a $15 credit toward your first month's on-line charges. 2. Check with your local software dealer or users' ...

    C++MFC教程

    Visual C++MFC入门教程 目录 +-- 第一章 VC入门 |------ 1.1 如何学好VC |------ 1.2 理解Windows消息机制 |------ 1.3 利用Visual C++/MFC开发Windows程序的优势 |------ 1.4 利用MFC进行开发的通用方法介绍 |----...

    Turbo C++ 3.00[DISK]

    special offer, and write for full details on how to receive a free IntroPak containing a $15 credit toward your first month's on-line charges. 2. Check with your local software dealer or users' ...

    Senfore_DragDrop_v4.1

    Earlier versions of Delphi and C++ Builder will not be supported. If you need Delphi 3 or C++ Builder 3 support you will have to revert to version 3.7 of the Drag and Drop Component Suite. The ...

    C++大学教程,一本适合初学者的入门教材(part1)

    第1章 计算机与C++编程简介 1.1 简介 1.2 什么是计算机 1.3 计算机组成 1.4 操作系统的变革 I.5 个人计算、分布式计算与客户/a匠务器计算 l. 6 机器语言、汇编语言和高级语言 1.7 C语言与C++的历史 1.8 C++...

    C++中sting类的简单实现方法

    析构函数,赋值运算符重载,运算符+=的重载,运算符[]的重载,c_str(得到一个C风格的字符指针,可操作字符串),Size,Push_Back,Insert(深拷贝),以及用写时拷贝copy_on_write的方式实现基本的String类 深拷贝的方式

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Doing Work in Constructors Default Constructors Explicit Constructors Copy Constructors Structs vs. Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control ...

    From C to C++

    // convert a string to uppercase! #include #define N 200 int main(){ char ms[N]; int i; printf("Input ms: "); gets(ms); for(i=0;ms[i];i++) if(ms[i]&gt;='a'&&ms[i]) ms[i]-='\x20'; puts(ms); ...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    FastReport® VCL is an add-on component that allows your application to generate reports quickly and efficiently. FastReport® provides all the tools necessary for developing reports, including a ...

    acpi控制笔记本风扇转速

    produced when this parameter was a null string (""). Now, the original input filename is used as the AML output filename, with an ".aml" extension. Implemented a generic batch command mode for the ...

    C++大学教程,一本适合初学者的入门教材(part2)

    第1章 计算机与C++编程简介 1.1 简介 1.2 什么是计算机 1.3 计算机组成 1.4 操作系统的变革 I.5 个人计算、分布式计算与客户/a匠务器计算 l. 6 机器语言、汇编语言和高级语言 1.7 C语言与C++的历史 1.8 C++...

    c/c++函数库说明(api)html版

    所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother...

    au3反编译源码

    To copy text or to enlarge the log window double click on it. Supported Obfuscators: 'Jos van der Zande AutoIt3 Source Obfuscator v1.0.14 [June 16, 2007]' , 'Jos van der Zande AutoIt3 Source ...

    一个跨平台的CString源码

    // the Standard C++ Library basic_string&lt;&gt; template and add to it the // the following conveniences: // - The full MFC CString set of functions (including implicit cast) // - writing to/reading ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    on a curve by the distance from the original point on the perimeter. - ADD: Added the method TCustomProp.GetPublishedComplexProp - returns included complex flex-property by name registered through ...

    Linux多线程服务端编程:使用muduo C++网络库

    2.8借shared_ptr 实现copy-on-write. . . . . . . . . . . . . . . . . . . . . . 52 第3章多线程服务器的适用场合与常用编程模型 3.1进程与线程. . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . ...

    RxLib控件包内含RxGIF,全部源码及DEMO

    TFormStorage allows you to read and write virtually any component published property to an INI file or the system Registry with virtually no code. Works with 3rd party and your own custom controls as ...

Global site tag (gtag.js) - Google Analytics