Intelligent pointers (in non-standard libraries) are widely used in your project, so there is no way to study them over the afternoon. Today is really a headache all day (it is estimated that Cao was so painful in that year. ^_^), an exception of more than nine points to go to bed...
Address: http://developer.51cto.com/art/201002/183350.htm
C ++ fully supports C functions. It not only supports these functions, but also optimizes these powerful functions. Here we will introduce the application methods of C ++ smart pointers for your convenience.
Memory leakage is a headache for C ++ programmers. C ++ lacks a powerful weapon such as JAVA and C #, which gives some permissions of memory management to programmers. Although the existence of GC saves development and troubleshooting time and cost, C ++ has never been added to its standards for 20 years in pursuit of operational speed. (Digress: C ++ increases the difficulty of development in exchange for execution speed. I don't know whether to give positive comments, but I 'd like to leave it to the future .)
Since then, C ++ programmers have been plagued by the problem of Memory leakage caused by the memory application on the stack and forgotten to be released. To make up for the high development threshold caused by no garbage collector, the C ++ libraries developed by major manufacturers are like COM learning to introduce SMART pointers to try to solve some existing problems.
The C ++ smart pointer is a class that stores pointer pointing to a dynamically allocated (HEAP) object. It is used for lifetime control and ensures automatic and correct destruction of dynamically allocated objects to prevent memory leakage. One of its general implementations is the use of reference count ). The smart pointer class associates a counter with the object to which the class points. The reference counting class tracks how many objects in the class share the same pointer. Each time a new object of the class is created, the pointer is initialized and the reference count is set to 1. when an object is created as a copy of another object, copy the constructor to copy the pointer and add the corresponding reference count. when an object is assigned a value, the value assignment operator reduces the reference count of the object indicated by the left operand (if the reference count is reduced to 0, delete the object), and increase the reference count of the object indicated by the right operand. when calling the destructor, the constructor reduces the reference count (if the reference count is reduced to 0, the basic object is deleted ).
Speaking of the C ++ smart pointer, we must look at the "funny" smart pointer: auto_ptr provided by the Standard C ++ library.
The standard library provides basic facilities for C ++ programs. Although the C ++ standard library has been tossing around with the C ++ standard for many years, it was not formally finalized until the standard was introduced. When I commented on the C ++ standard library online, I said: "The implementation of the standard library is very pleased to see a variety of implementations, and has been proven to be an industry-level success." However, in the current standard C ++, there is only one unique smart pointer: std: auto_ptr.
The auto_ptr pointer is a RAII object that obtains resources during initialization and automatically releases resources during destructor (end of life). There are countless disadvantages:
1. auto_ptr requires only one owner for an object.
2. Lack of support for the number of references and arrays.
3. auto_ptr objects cannot be used as elements of STL containers. The C ++ standard explicitly prohibits such operations, otherwise unexpected results may occur. (This film is dizzy ).
4. auto_ptr transfers ownership when being copied.
It can be seen that the smart pointer of the standard library is useless.
In this case, the C ++ Standards Committee naturally needs to consider introducing new smart pointers. At present, a Boost smart pointer series is developed by the Boost organization sponsored by the C ++ standard committee Database Working Group.
In Boost, there are five types of C ++ smart pointers: scoped_ptr, scoped_array, shared_ptr, shared_array, and weak_ptr.
The first four solutions are proposed for auto_ptr in the standard library. For example, scope_ptr is proposed for the weakness that "auto_ptr transfers ownership when being copied. I have never seen the last one. It looks like a weak reference smart pointer. I suspect it is similar to a weak reference in JAVA. Further study is required.