Standard C class std :: string
Memory Sharing and Copy-On-Write Technology
Chen Hao
1, concept
SCOTT Meyers took an example in "More Effective C ", I don't know if you still remember? When you are still going to school, your parents want you to watch TV, and go to review your homework, so you will close yourself in the room, make a look that you are reviewing your homework, in fact, you do other things like give A girl in the class is a matter of writing a book, and once your parents come in your room to check if you are review, you really pick up the textbook. This is "delay tactics" until you don't do it.
Of course, this kind of thing often occurs in real life, but it has become the most useful technology in the programming world, as in C can declare the variables, Scott Meyers recommended us, real needs When a storage space is declared (allocated memory), this will get the program's smallest memory sales at runtime. Performing to the time-consuming job that will be done to allocate memory, this will give us a better performance when running. By, 20% of the program runs 80% of time.
Of course, the delay tactics are not just such a type, which is widely applied by us, especially in the operating system, when a program runs, the operating system does not hurry to clear the memory, The reason is that there is a possible program to run once again (putting the program from the disk to memory is a very slow process), and only when the memory is not enough, these still resident a program.
Copy-on-write technology is the product of "lazy behavior" - delay tactics. For example, if we have a program to write files, constantly write according to the data from the network, if each FWRITE or FPRINTF wants to make a disk I / O operation, it is simply a huge loss. Therefore, the usual approach is that each write operation is written in a single memory (disk cache), only when we turn off the file (this is why if the file is not closed, The reason why things will be lost). What's more, it is not written when the file is closed, and it will wait until the shutdown or memory is not enough to write a disk. Unix is such a system. If it is not normal to exit, then the data will be lost, and the file will be damaged.
Oh, for performance, we need to take this big risk, and our program will not be busy forgetting that there is still a piece of data to be written to disk, so this approach is still necessary.
2, standard C class std :: string Copy-On-Write
The String class in the STL Standard Template library we often use is also a class with a copy of the technology. C has been widely questioned and accused in performance issues. In order to improve performance, many classes in STL use Copy-ON-WRITE technology. This lazy behavior does make a relatively high performance of the program using STL.
Here, I want to uncover the veil implemented in String from the angle of the C or design mode, to do a point when designing C class design.
Before telling this technology, I want to simply explain the concept of String class memory allocation. By often, there must be a private member in the String class, which is a char *, and the user records the address of the memory from the heap, which allocates memory when constructing, and releases memory during construction. Because it is allocated from the pile, the String class is carefully carefully, and the String class returns const char * when the String class returns this memory address, which is read-only if you want to write, you Data can only be rewritten by the method provided by String. 2.1, characteristics
From the table and in the form, let's take a look at the surface characteristics of the String class's Copy-ON-WRite. Let's write down the following programs:
#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 make the second String through the first String constructed, then print the memory address of its storage, then modify the contents of STR1 and STR2, and then check the address of the memory. 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 starting two statements, STR1 and STR2 store the address of data, and after modifying content, the address of STR1 changes, and the address of Str2 is still. From this example, we can see the COPY-ON-WRITE technology of the STRING class.
Next->
(All rights reserved, please indicate the author and the source when reproduced)