Copy-on-write technology for standard C + + class string (i)

Source: Internet
Author: User
Tags first string

Turn from: http://blog.csdn.net/haoel/article/details/24058

Standard C + + class std::string

Memory Sharing and Copy-on-write technology

Chenhao 1, Concept

Scott Meyers in "more effective C + +" an example, I do not know whether you remember. When you were still at school, your parents asked you not to watch TV, but to review your lessons, so you lock yourself in your room and make a review of your lessons, but you're doing something else like writing a love letter to a girl in your class, and once your parents come out in your room to check if you're reviewing, You really picked up your textbook and read it. This is "delaying tactics" until you have to do it.

Of course, this kind of thing often happens in real life, but it becomes the most useful technology in the programming world, just as it is possible to declare variables everywhere in C + +, Scott Meyers recommends that we declare variables (allocating memory) when we really need a storage space. This will get the minimum memory cost of the program at run time. Execution to that will be done to allocate memory this time consuming work, this will give our program to run a better performance. Must have, 20% of the program runs 80% of the time.

Of course, delaying tactics is not just such a kind of technology that is widely used by us, especially in the operating system, when a program is finished, the operating system is not anxious to clear out the memory, because it is possible that the program will run again immediately (from disk to load the program into memory is a very slow process), And only when the memory is not enough, will these also reside in the memory of the program to clear out.

Write-only copy (Copy-On-Write) technology, is the programming community "lazy behavior"-delaying tactics of the product. For example, we have a program to write files, constantly based on the data from the network to write, if each fwrite or fprintf to do a disk I/O operations, is simply a huge performance loss, so the usual practice is, Each write-file operation is written in a particular sized piece of memory (disk cache) and is written to disk only when we close the file (which is why if the file is not closed, what is written will be lost). What's more, when the file is closed without writing a disk, and wait until the shutdown or memory is not enough to write the disk, UNIX is such a system, if the abnormal exit, then the data will be lost, the file will be damaged.

Oh, in order to performance we need to take such a big risk, fortunately our program is not too busy to forget that there is a piece of data need to write to disk, so this practice, or very necessary.

2, the standard C + + class std::string Copy-on-write

The string class in the STL Standard Template Library that we often use is also a class that has a write-only copy technique. C + + has been widely questioned and blamed on performance issues, and in order to improve performance, many of the classes in STL adopt Copy-on-write technology. This lazy behavior does make the use of STL programs have a relatively gaoyao performance.

Here, I would like to from the C + + class or design mode for you to uncover the Copy-on-write technology in string implementation of the veil, for you to use C + + for class library design to do a little reference.

Before I tell you about this technique, I'd like to briefly explain the concept of string class memory allocation. Often, the string class must have a private member, a char*, where the user records the address of the memory allocated from the heap, allocates memory at construction time, and frees up memory at the time of the destructor. Because the memory is allocated from the heap, the string class is extremely cautious in maintaining this block of memory, and the string class returns only the Const char*, which is read-only, if you want to write, you can only overwrite the data with the method provided by string.

2.1, Characteristics

Youbiaojili, from perceptual to rational, we first look at the surface characteristics of the copy-on-write of the string class. Let's write down the following procedure:


#include

#include

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;

}

The intent of this program is to have the second string constructed from the first string, then print out the memory address where the data resides, and then modify the contents of the str1 and str2, and then check the address where the memory resides. The output of the program is like this (I got the same result in VC6.0 and g++ 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

From the results we can see that after the first two statements, STR1 and str2 the same address for the data, and after modifying the content, STR1 address changed, and STR2 address is original. From this example, we can see the copy-on-write technique of the string class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.