From: http://hi.baidu.com/zoupng/blog/item/becaa4f42e1d396bdcc474fb.html 2.4, bug
Who said, "Where there is a sun, there will be darkness "? Perhaps many of us are superstitious about standard things and think that they have been tested for a long time and cannot make mistakes. Well, do not be superstitious, because any well-designed code or code may have bugs in a specific situation. The same is true for STL, the shared memory/Write-only copy technology of the string class is no exception, and this bug may cause your entire program to crash!
Believe it ?! Let's look at a test case:
Assume that there is a dynamic link library (mynet. dll or mynet. So) that such a function returns the string class:
String getipaddress (string hostname) { Static string IP; ...... ...... Return IP; } |
While your main program dynamically loads this dynamic link library and calls this function:
Main () { // Load functions in the dynamic link library Hdll = loadlibraray (.....); Pfun = getmodule (hdll, "getipaddress "); // Call functions in the dynamic link library String IP = (* pfun) ("host1 "); ...... ...... // Release the dynamic link library Freelibrary (hdll ); ...... Cout <IP <Endl; } |
Let's take a look at this Code. The program loads the functions in the dynamic link library dynamically, calls the functions in the dynamic link library using the function pointer, and places the returned values in a string class, then the dynamic link library is released. After the IP address is released, enter the content of the IP address.
According to the definition of the function, we know that the function is "Return Value". Therefore, when the function returns, it will certainly call the copy constructor, and according to the Memory Sharing Mechanism of the string class, in the main program, the variable IP address shares the memory with the static string variable in the function (This memory zone is in the address space of the dynamic link library.). However, we assume that the IP address value has not been modified throughout the main program. Then, when the master program releases the Dynamic Linked Library, the shared memory zone is also released. Therefore, in the future, access to IP addresses will inevitably lead to illegal access to memory addresses, resulting in program crash. Even if you do not use the IP address variable in the future, memory access exceptions may occur when the main program exits, because the IP address will analyze the structure when the program exits, memory Access exceptions may occur during structure analysis.
Memory Access exceptions mean two things: 1) No matter whether your program is beautiful, it will become invisible because of this error, and your reputation will also be lost because of this error. 2) In the future, you will suffer from this system-level error (in the C ++ world, it is not easy to find and eliminate this memory error ). This is the heart pain of C/C ++ programmers forever. It is a treasure of a thousand miles, and it is broken by the ant nest. If you do not know the features of the string class, finding such a Memory exception in thousands of lines of code would be a nightmare.
Note: There are many methods to correct the above bug. Here is a method for reference only:
String IP = (* pfun) ("host1"). CSTR ();
3. Postscript
The article should end here. The main purposes of this Article are as follows:
1) introduce the copy/Memory Sharing Technology During writing.
2) taking the string class in STL as an example, we introduce a design pattern.
3) in the C ++ world, no matter how elaborate your design and code are, it is hard to take care of all the situations. Smart pointers are a typical example. No matter how you design them, there will be very serious bugs.
4) C ++ is a double-edged sword. Only by understanding the principles can you better use C ++. Otherwise, you will be taken over. If you have a feeling of "playing C ++ is like playing with fire, you must be careful" when designing and using a class library, you will get started, when you are able to control the fire, you can learn it.
Finally, I will introduce myself using this post-order. I am currently engaged in software development on all UNIX platforms, mainly for system-level product software development. I am very interested in the next generation of computer revolution-grid computing, I am also very interested in distributed computing, P2P, Web Service, and J2EE technologies. In addition, I have little experience in project implementation, team management, and project management, I hope that the young generation who fought with me on the "technology and management" front will be able to communicate with me a lot. My MSN and mail are: haoel@hotmail.com.
My column is:
Http://blog.csdn.net/haoel/