2.2, in-depth
In the past, through the above demonstration, we should know that in the String class, you should copy the copy, you need to solve two problems, one is a memory share, one is a copy-on-wirte, these two themes will make We have a lot of questions, or let us learn such a few questions:
1, what is the principle of Copy-on-Write?
2, what is the STRING class shared memory?
3. What is the case when the STRING class triggered? Copy-ON-WRITE?
4, when Copy-on-Write, what happened?
5. What is the specific implementation of Copy-ON-WRITE?
Hey, you say that just look at the STIRNG source code you can find the answer. Of course, of course, I also refer to the source code of String's parent template class Basic_String. However, if you feel that the STL source code is like watching the machine code, and seriously combat you with C self-confidence, and even if you have a question of C , if you have this feel, then continue to look down on me. Article.
OK, let us discuss a question, slowly all technical details will come out.
2.3 What is the principle of Copy-ON-WRITE?
Programmer with a certain experience must know that Copy-on-Write must use the "reference count", yes, there must be a variable similar to the refcnt. When the first class configuration, the String constructor allocates memory from the heap according to the incoming parameters. When there is any other class that requires this memory, this count is automatically accumulated. When there is a class destructor, this count will Dimensiony one, until the last class destructor, the REFCNT at this time is 1 or 0, at this time, the program will really be allocated from the heap.
Yes, the reference count is the principle of copying when the String class is written!
However, the problem is coming, where is this refcnt? If you are stored in a String class, then each String instance has a certain set, you can't have a REFCNT at all, if it is declared to a global variable, or a static member, that is, all String class share one, this is also No, we need a solution to "democracy and concentration". How is this doing? Oh, life is a confused to detect, know the recirculation process after the back and confused. Don't worry, I will give you one by one behind.
2.3.1 What circumstances do you share memory?
The answer to this question should be obvious. According to the common sense and logic, if a class is to use another class, you can share the memory used by the class. This is reasonable, if you don't need me, then you don't have to share, only you use me before you share.
When using the data of other classes, it is nothing more than two cases. The first case is triggered, and the copy constructor is triggered, and the second case triggers the assignment operator. These two cases we can implement their corresponding methods in the class. For the first case, you only need to do some processing in the copy constructor of the String class, allowing it to quote counting; A little process.唠叨 a few words:
1) The difference between constructing and assignment is these two sentences in the front routine: string str1 = "hello world";
String str2 = STR1;
Don't think that "=" is assignment, in fact, these two statements are equivalent to: String str1 ("Hello World"); // Calling constructor
String str2 (str1); // Calling a copy constructor
If STR2 is the following: String str2; // Call the parameter default to the constructor of the air string: String STR2 ("");
STR2 = str1; // Call the assignment of STR2: str2.operator = (str1);
2) Another situation char TMP [] = "Hello World";
String str1 = TMP;
String str2 = TMP;
Do you trigger a memory of memory in this case? If you think of course, you should share it. However, based on the shared memory mentioned earlier, the declarations and initial statements of the two String classes do not meet the two cases mentioned foregoing, so they do not have memory sharing. Moreover, C existing features cannot let us do the memory sharing of this situation.
2.3.2, what situation is triggering when it triggers a copy (Copy-Write)?
Oh, when will I find it when I write? Obviously, of course, Copy-ON-WRITE will occur when the class occurs when the class is shared with the same memory. For example, the String class [], =, =, , operator assignment, and some member functions such as INSERT, REPLACE, APPEND, including class destructor.
Modifying data will trigger Copy-on-Write, which does not change it. This is the true meaning of TOEON war, not going to do it.
2.3.3, when Copy-on-Write, what happened?
We may decide whether to copy if you need to copy, see the code below:
IF (Refcnt> 0) {
Char * TMP = (char *) Malloc (Strlen (_PTR) 1);
STRCPY (TMP, _PTR);
_PTR = TMP;
}
The above code is an imaginary copy method, if there are other classes in the reference (check reference count) this memory, then you need to "copy" this action.
We can put this copy of the runtime to a function for those who change the contents of the content.
<- Previous Next->
(All rights reserved, please indicate the author and the source when reproduced)