lunch time.
Zero sat in front of the table, the machine's repeat "clips -> chew -> swallow" action sequence, the face is written with an invisible big character: I am not here. Sitting Solmyr in his opposite side, slowly eating his lunch, maintaining him consistently a very cultivated image - or by Zero these familiar people: illusion.
"What's wrong with Zero? What is good?" After basics, SolmyR felt that he should be careful about his apprentice.
"Oh, there is nothing, just ... Solmyr, C why does not support garbage collection? (Note: garbage collection is a mechanism to ensure that the dynamically allocated memory block will automatically release, Java and other languages support this mechanism.)"
SolmyR sighed, stared at Zero with a calm eye: "Is it better in BBS and a human C and java which better? And noisy? I told you that this argument is bored. "
"Oh ... Yes", Zero has to admit that although the eyes of SolmyR is not sharp, but inexplicably make ZERO a slight fear.
"And, who tells you that C does not support garbage collection?"
"Ah! Solmyr, are you not a joke ?!"
"Zero, you have to change the concept. I ask you, C does not support the array of dynamic changes in size?"
"This ... is there really?"
"What is the vector?"
"Uh ……"
"Support a feature, not to add this feature to the syntax, we can also choose to implement a library with an existing language mechanism to support this feature. Take garbage collection as an example, here our task is to guarantee Every memory block that is dynamically allocated can be released, that is, ... ", SolmyR I don't know where to find a piece of paper, a pen, write:
INT * P = new int; // 1 delete P;
"That is to say, for each 1, we must ensure that there is a 2 call, 1 and 2 must be paired. I will ask you, what is there in C is guaranteed by the language itself?"
"...", Zero reveals the expression of efforts to search for memory, but it is very clear.
"Tips, and related to the creation of the class."
"Oh! Constructor and the destructor!"
"Correct. Unfortunately, the ordinary pointer does not construct the function and the destructor, so we have to write a class to add a layer of packaging, the simplest is like this:"
Class my_intptr {public:
MY_INTPTR (INT * P) {m_p = p;} _intptr () {delete m_p;}};
..........
MY_INTPTR PI (New Int); * (pi.m_p) = 10;
..........
"Here we can safely use my_intptr, do not worry about the problem of memory leak: Once this variable is destroyed, we know that the memory pointer points to PI.P will be released. However, if you use my_intptr to access its member every time you use my_intptr. It is too much trouble. To this end, you can add overloaded * operators to this class: "
Class my_intptr {private:
PUBLIC: MY_INTPTR (int * p) {m_p = p;}
..........
MY_INTPTR PI; * Pi = 10; int A = * pi;
..........
"It is like a true pointer now is a true pointer? For this reason, this technology is called a smart pointer. Now I ask you, what is the lack of this class?"
Zero frowned, eyes, look like a slow computer is working hard to copy files on its hard drive. For a long time, Zero lifted his head, not to be sure: "Is it still lacking a copy constructor and an assignment operator?"
"Tell why.", SolmyR is obviously not intended to put it in Zero.
"Because ... I remember that it is true ..." 50 "(Note: Refers to the" Effective C 2 / E "book) mentioned that if your class is pointed to dynamically allocated memory, then you must Write a copy constructor and an assignment operator ... Because ... Otherwise, once you assign a value, it will cause the two object's pointer to the same memory. Right! If it is the above class, this way It will cause the same pointer to be DELETE twice! "
"Correct. What should we achieve?"
"This is simple, we use memcpy to copy the contents of the memory pointer to the contents of the memory."
"What if our smart pointer points to a class object? Note that there may be a pointer in the object, you can't use Memcpy."
"That ... we use a copy structure."
"If our smart pointer points to the object not to copy the structure? It may have a private copy constructor."
"That ...", ZERO has a meal, decided to be honest, "I don't know."
"How do you know? What do you know? If you don't think of a smart pointer as a pointer. Imagine, if we assign a pointer, what is its meaning?"
"Hey, I understand, in this case, you should find a way to let the two smart poors points to the same object ... but Solmyr, so don't I still delete the same object twice?"
"Yes, we have to find ways to solve this problem, the way is not only one. One of the better is to maintain a reference count value for each pointer, each assignment or copy construct, let the count value plus one, which means The smart pointer points to this memory block has been one; and every smart pointer is destroyed, let the count value minus one, which means a smart pointer to this memory block; once the count value is 0, it will be released The memory block. I like this: "
Class my_intptr {private:
PUBLIC: MY_INTPTR (INT * P) { M_P = P; m_count = new int; // Initialization count value is 1 * m_count = 1; } my_intptr (const my_intptr & rhs) // copy constructor {m_p = rhs.m_p; // points to the same memory m_count = rhs.m_count (* m_count) -; // count value reduction 1 IF (* m_count == 0) delete m_p; delete m_count; } } my_intptr & operator = (const my_intptr & rhs) {if (m_p == rhs.m_p)
(* M_count) -; / / / / There is no other pointer to point to the original memory block { delete m_p; delete m_count;}}
M_P = rhs.m_p; // Point to the same memory m_count = rhs.m_count; // Using the same count value (* m_count) }
............}
"There is no big change in other parts. I don't expect to use this smart pointer now." SolmyR put down the pen, pick up the chopsticks again, some regretful found that the meatballs that he loved was cold. .
Zero imagined, some hesitated. "We ... can construct a smart pointer with the new int expression as the parameter of the constructor, then ... then we can arbitrarily assign a value,", he began to grasp the ideas, the more you say, "Arbitrary use already The existing intelligent pointer to construct a new smart pointer, the intelligent pointer's assignment operator, the copy constructor, and the destructure ensure that the count value is always equal to the number of intelligent poors that point to the memory block. "Zero seems to understand what the function he saw. Start excitement: "Then once the count value is 0 is allocated, the memory block will be released! That is to say ... Have a pointer points to the memory block, it will not release it, once it doesn't, it will be released automatically! Great! We As long as the correct initialization smart pointer is used, you can use it as a normal pointer, and you don't have to worry about the problem of memory release! "Zero excited:" This is garbage collection! SolmyR! We are on the dinner table A garbage collector! "
SolmyR is obviously not shared by Zero: "I am eating, can you not call the 'dinner table to achieve a garbage collector' this kind of appetite?" Solmyr with his signature Laugh, said in a hateful tone: "And please pay attention to your own image."
"Well?", Zero came back to God, found that he didn't know when he stood up, and people in the entire restaurant were watching him, which made him feel like a fool. Zero red face sat down, and smashed the sound asked Solmyr: "But Solmyr, this is indeed a garbage collection mechanism, as long as we change this class into ... um ... it is changed to template, like this:" Zero After the paper pen, write:
Template
"Is it possible to support any type of pointer? We can use it anywhere."
SolmyR shook his head: "No, you think too simple to think. For simple types, this class can do very well, but the actual situation is very complicated. Consider a typical situation: Class Derived is a class Base Derive class, we hope to assign this: "
Base * Pb; Derived Pd; ............ Pb = Pd;
"You talk about it, this situation, how to use this smart pointer to handle?"
"...", Zero is silent.
"It is not easy to achieve a complete garbage collection mechanism, because there are many details to consider." Solmyr begins to summarize it, "However, basic ideas are these. It is worthy of fortunately, there is already a quite mature The 'reference count' smart pointer, Boost :: Shared_Ptr. In most cases, we can use it. In addition to the smart pointer, there are some technologies that can help us avoid memory issues, such as memory pools. However, the key is --- "
SolmyR stares at Zero with the calm eyes.
"As a C / C programmer, there must be creativity. The kind of person who is not thinking about in the language mechanism, that must rely on grammar to know how to program the program, that kind of no one tells him what to do Nothing people, not suitable for this language. "