Copy-on-write)

Source: Internet
Author: User

Original address:

Http://hi.baidu.com/jakisou/blog/item/255e9cd66f16a72a06088b20.html

 

1. Concepts

 

Scott Meyers gave an example in more effective C ++. Do you still remember this? When you are still at school, your parents want you not to watch TV, but to review your lessons, so you keep yourself in the room and make a look at your homework, in fact, you are doing other things, such as writing love letters to a girl in the class. Once your parents come out in your room and check whether you are reviewing, only then can you really pick up textbooks and read books. This is the "Procrastination tactic", which is not done until you have to do it.

 

Of course, this kind of thing often happens in real life, but it has become the most useful technology in the programming world, just as variables can be declared everywhere in C ++, Scott Meyers recommends that you declare variables (allocate memory) only when you really need a bucket ), in this way, the minimum memory consumption of the program during running is obtained. The time-consuming work such as allocating memory will be done only after execution, which will give our program better performance during running. 20% of the programs run for 80% of the time.

 

However, the procrastination tactics are not just such a type. This technology is widely used, especially in the operating system. When a program stops running, the operating system does not rush to clear the memory, because it is possible that the program will run again immediately (loading the program into the memory from the disk is a very slow process ), only when the memory is insufficient can these programs that still reside in the memory be cleared out.

 

The copy-on-write technology is the product of the "lazy behavior" in the programming world-delaying tactics. For example, we have a program that needs to write files and constantly write data from the network, if I/O operations on a disk are performed on each fwrite or fprintf operation, it is simply a huge performance loss. Therefore, the common practice is, each file write operation is written in a memory (disk cache) of a specific size. Only when we close the file is written to the disk (this is why if the file is not closed, the cause of the loss of the written content ). What's more, when files are closed, they are not written to the disk, but they are written to the disk until they are shut down or the memory is insufficient. UNIX is such a system. If the system unexpectedly exits, data will be lost, the file is damaged.

 

Well, we need to take such a big risk for the sake of performance. Fortunately, our program won't be too busy to forget that another piece of data needs to be written to the disk. Therefore, this practice is still necessary.

 

 

2. Standard C ++ STD: String Copy-on-write

 

The string class in the STL standard template library that we often use is also a class with the copy technology when writing. C ++ has been widely questioned and accused of performance issues. To improve performance, many classes in STL adopt the copy-on-write technology. This kind of lazy behavior does make STL programs have relatively high performance.

 

Here, I want to unveil the implementation of copy-on-write technology in string from the perspective of C ++ classes or design patterns, for your reference when using C ++ for class library design.

 

Before talking about this technology, I would like to briefly describe the concept of memory allocation for the string class. Normally, there must be a private member in the string class, which is a char *. The user records the address of the memory allocated from the stack. The user records the memory allocated during the construction and releases the memory during the analysis. Because the memory is allocated from the heap, the string class is very careful in maintaining this memory. When the string class returns this memory address, it only returns const char *, that is, read-only, if you want to write data, you can only rewrite the data using the method provided by string.

 

2.1. Features

 

From the table to the inside, from the sensibility to the rationality, let's take a look at the copy-on-write surface features of the string class. Let's write down the following program:

 


# 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 intention of this program is to let the second string be constructed through the first string, then print out the memory address of the data, and then modify the content of str1 and str2 respectively, check the memory address. The program output is as follows (I got the same result in vc6.0 and G ++ 2.95 ):

 


> G ++-O 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, the addresses of str1 and str2 storing data are the same, and after the modified content, the address of str1 has changed, the str2 address is still the original one. In this example, we can see the copy-on-write technology of the string class.

 

 

Copying During writing can improve the efficiency of the STL library, but it is easy to encounter some traps when we use it.

 Int main () <br/>{< br/> string str1 = "ABCD"; <br/> string str2 = str1; <br/> char * P1 = const_cast <char *> (str1.c _ STR (); <br/> P1 [0] = 'O '; <br/> // str1 and str2 are modified at the same time <br/> printf ("% S % s/n", str1.c _ STR (), str2.c _ STR (); <br/> return 0; <br/>}

Looking at this example, this is a problem shared by others. The intention is certainly not to modify the str2 value, but due to STL's opinion

Char* P1 = const_cast <Char*> (Str1.c _ STR ());

The str1.c _ STR () operation returns the const char * object, which does not cause write operations. Therefore, the str1 object is not copied, whereas the const_cast <Char*> After being forcibly converted and assigned to P1, subsequent write operations lead to tragedy.

 

After analyzing the cause, it is easy to solve the problem. before writing, STL has copied the memory of the object, and the method is wise.

 

 

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.