Resource management has been a place where C + + is criticized by other languages, such as Java. It is undeniable that garbage collection is a more advanced and efficient means of managing resources, and because of its internal algorithm improvement, its speed and efficiency is gradually improved. But, after all, garbage collection allows programmers to take a step further away from the system, not only to make programmers less aware of what's going on Inside the program (which is why I learned Java but not to use it), but also to run the garbage collection program at a rate that's not as good as manual management.
With the promotion of C++11, resource management has not been so difficult (c++11 can use the Boost library to achieve this effect), new/delete so difficult to work together basically can not, because we can use more efficient and convenient standard library and Smart pointers .
First, for knowledge of building objects on the stack, refer to the following link http://www.cnblogs.com/sopc-mc/archive/2011/11/03/2235124.html.
Now, we don't need to be new every time, then we have to remember to delete (otherwise memory leaks). All we have to do is hand over the object that we originally wanted new to, and then we don't have to be afraid of the memory leak because we forgot the delete, because the smart pointers have been managed for us. So how does a smart pointer do that? In fact, the use of raii (Resource acquisition is initialization : Resource timing is the time of initialization) , this raii actually uses the class object to leave the scope and automatically calls the destructor to release the resource , such as:
1 class local { 3 ... }; 6 void F () 7 { 8 ... 10 }
when the object L leaves the function f (), it automatically calls the destructor to release the resource . Raii want is this automatic (automatic is the key!) Call the effect of the destructor! The smart pointer is actually a class that hosts the resource, and when you need to release the resource, he can automatically invoke his own destructor.
Next is the problem of the new array, we have to deal with the array of pointers, the pointer is too naughty, I am afraid we do not like to play with it, and now we have to deal with the problem of New/delete, which makes the head is big. Fortunately we also have a strong standard library, when we use vector and string to solve the problem of 90% array, and these two things are so easy to use! In fact, modern C + + programming has not had to deal with pointers all day, using a good standard library (and the Boost library!). , C + + programming really becomes efficient and convenient.
Here, we can summarize: (1) for a single object, with a smart pointer;
(2) for an array of objects, use the vector and string of the standard library.
Finally, I quote Scott Meyers to conclude this article: "In a perfect world you will rely on such classes to deal with all interactions with resources, rather than defile the hands directly with the original resources."
Resource management of C++11 (I.)