"Zero help ~~"
I heard this full sincere voice, ZERO collapsed the shoulders, I feel that I am in the edge of a completely collapse -, in a few months, every time the Pisces encounters what can't be solved, I always use this opening to ask ZERO, Then Zero has to face a weird problem of all kinds of incompetence.
"How can she make so many mistakes?" Zero launched in his heart. He bitterly face, asked: "You can't let me go to SolmyR?"
"But SolmyR specifies you to help us!"
"... Ok, let's talk about what the problem", Zero once again hurt Solmy Rethers one hundred times, and did it again to face the strange mistake.
"Well, there is a code here to read the configuration file information. Now just a framework, you may need to read some configuration files in the future, and may be placed in different directories, so I use some String object to save the path and file name, Then open with Fopen. "Said, Pisces tested a CPP file:
#include
Using namespace std;
Const string path = "./cfg/";const string name =" system.cfg ";
// Parameter is a pointer // return value to the Save Configuration Information Structure as the success mark, TRUE represents success, FALSE represents failed BOOL READ_CFG (CFG_Data * p_data) {const string path_name = path name; file * fp = fopen (path_name. C_STR (), "R"); if (fp == null) Return False;
// read the configuration file using the FP, put it in the CFG_DATA structure ... ... ...
Return True;}
....
"When I run it here, but when I submit it to the test group to do unit test, I always have a problem, saying that I can't find a configuration file, I have to go there, I have seen it, the name and path of the configuration file are right. Yeah, it's too strange. "
Zero took a look at this code, which is very dissatisfied with the style: "I said Pisces, the wrong is not mentioned, your code style is not very like C . First parameter should be referenced, its security Sex is much higher than the pointer, and since you use the pointer, you should check it with the assertion; after you do it, do you have a message to read the file? Do you want to use fopen ... "
"I know" I know! "Pisces interrupted Zero," I know that my style is not very good, but no matter how this open file is not wrong, it is not wrong? Now the problem is an error when it is open. Obviously the document is there, the function is always returning false. Help me find this bug first, help! ~~~~~ "
"Good!", Zero shook his head and start looking for errors. For 5 minutes, Zero's brow gradually wrinkled, and only three five lines of code look at it, and there is no hidden place where it is hidden. ZERO introduces this code into the project he uses to test, compiles the connection, test operation, and all the procedures are normal, and the configuration file is found correctly. How is this going? Zero confused. "Zero?" Pisces turned the eye of the inquiry to Zero. "Hey ... don't worry, let's go to the test group to see what is going on." Zero is lucky, it is ok to make a mistake? " After 10 minutes, Zero's heart was turned back, followed by Pisces. The test group did not make a mistake. In their code, the code did not work, the debugger displays, the file name is incoming FOpen is an empty string. How is this going? On the side of Zero, I walked to my own seat - what? The person standing in front of his computer is not ... isn't ... SolmyR? His hand is ...
ZERO instinct is dangerous, a mighty one! A folder wiped the past from the place where only 0.01 cm from his face! Unfortunately, the back Pisces is not so lucky, being hit!
Solmyr looked at the Pisces who was looking at his face, asked Zero, scared,: "The code on the screen is not what you wrote?"
"Yes, it is written." Zero looked at Pisces sympathy, the latter seems to have not recovered from the hit ...
"There is a result, it is really wrong, it is not wrong ..." Solmyr shrugged, "Do you know why this code is wrong?"
Zero smiled: "Don't know."
"Let your eyes let go, you will know if you call this function."
ZERO has tested out the entire project. According to the documentation, read_cfg is one of the steps of the entire system initialization process. When the system starts, the configuration file is read, and some initialization parameters are determined. According to this, Zero is easy to find the call, in another CPP file:
#include "config.h" // declares that read_cfg this function
Class System {public: // Complete the initialization work when the system starts system () {cfg_data data; read_cfg (& data);
// Use the information configuration system in DATA ...}
/ / Complete the cleaning work when the system exits ~ system () {...}
"
SYSTEM THESYSTEM; // Represents a global object of the entire system
SolmyR clearly cleared: "This is a very clean way. System is only a global object here. This global object represents the entire system, its structure, system is initialized; it sects, the system begins to exit Cleaning work. The identity of the global object guarantees that it will be constructed before entering the main function. After the main function exits it. This trick is you teaching Pisces? "Solmyr looked at Zero.
ZERO nodded: "Yes, last time she asked me how to deal with the initialization and clean code, I remembered the last time about 'into a discussion (Note), I gave her out this idea. "
"The idea is the head, but the implementation method is not properly, the problem is above the global object. I ask you, a CPP file, or formally, what is the order of the global object construct in a compilation unit? "" "... should be in order to define their order." Zero's memories of Memory, very certain.
"Correct, then the sequence of construction between the global objects between different compilers?"
"... It seems that there is no clear rule, should this be a standard undefined right?"
"Correct, so ..."
"So ... ... ah! I understand! Oops! How can I be so slow! I will think of it when I have passed the empty string!" Zero revealed the expression that suddenly realized, "in opening the configuration file In the code segment, the Path and Name saved file name are also global objects. In other words, the two String objects and the construction of the THSYSTEM object are unable to determine. In the test group, the THESYSTEM is constructed first in Path and Name, so When the THESYSTEM constructor calls the read_cfg function, the two String objects of Path and Name have not been constructed yet! Of course, the file name and path are not removed! And on the computer of me and the Pisces, this paragraph The code can run correctly. "
"Very good, how can I solve it?"
"Well ... ... I want to avoid the global object as much as possible, on the one hand, the object can put it in the main function, so you can ensure that initialization work and cleanup work correctly; on the other hand, Read_CFG is best Do not use the global String object, you can use local objects. This is able to solve this problem ... "Zero frowned, obviously not satisfied with this solution," If I have some global nature, And it is desirable to precisely define their constructive order, what should I do? "
SolmyR is nod: "Yes, sometimes this is really a reasonable requirement. The simplest solution to this is' STATIC object" by the function package "(Note), like this:"
System & Shensystem () {static system instance; return instance;}
"INSTANCE is a Static object, which guarantees its survival, then it will construct when this function is called. For your problem, as long as you declare multiple such functions, then ensure they first call Order, you can guarantee the order of these objects. "
Zero nodded if he thought.
"Say, the order problem is absolutely not only this one, and there is similar problem in C . The closest question is the closest, and the sequence of the class, the sequence of other expressions, and the function parameters Ask for the order, (note three), etc., in order, don't want to think about it, ask yourself: Is there a definition in this order? It is defined to abide by, there is no definition to avoid. Ok, this problem It is probably the case, the next task is ... "
"I know, put these discussions into documents?"
"No, I want to let Pisces understand this problem."
"... ... ...... ... ...... ..."
Looking at the back of SolmyR's hurried, then looked at the "sincere" eyes around him looked at their Pisces, Zero suddenly raised a strange feeling: It seems that it is just simming that SolmyR Not so bad ... ... ...... =====================================
Note 1: About "Demonstration", please refer to the "Solmyr's small piece of text series:"
Note: You can refer to the 10.4.9 section of the C programming language (special edition) ---- In fact, if you look at the section, this article is mostly nonsense, ^ _ ^
Note 3: The discussion of these issues can be seen "Effective C 2 / E" Terms 13, GotW Terms 12, and ... Other all ... ^ _ ^ b