C ++ smart pointer and smart pointer
C ++ smart pointers are encapsulated by a layer based on common pointers, making it easier and easier for users to use pointers, you do not need to worry about exceptions caused by pointer release during usage. In C ++ 11, there are three types of smart pointers: shared_ptr <T> ptr, unique_ptr <T> ptr, weak_ptr <T> ptr; shared_ptr <T> ptr can be initialized in the following ways: 1) shared_ptr <T> ptr = make_shared <T> (args ); // The args parameter format is the same as that of the T constructor. 2) shared_ptr <T> ptr (q); // q can be a smart pointer or a common pointer (which can be converted to T *, for example, & v ), it can also be a new memory allocated. 3) shared_ptr <T> ptr = q; // q can be a smart pointer or a general pointer, but not a new memory. 4) shared_ptr <T> ptr (u); // u is a unique_ptr. At this time, u is set to null, and ptr points to the object pointed to before u. 5) shared_ptr <T> ptr (q, d); // similar to method 2, however, the new delete function d will replace the delete function, but it is better not to mix the smart pointer with different pointers. Other operations, such as: 1) ptr = q; // ptr points to the object pointed to by q. q must be a smart pointer. The template types of ptr and q are not necessarily the same, as long as they can be changed from the template type of q to the template type of ptr. In this case, the reference count of the object to which ptr originally points is reduced by 1, and the reference count of the object to which q points is increased by 1. 2) ptr. unique (); // returns whether the number of object references pointed to by ptr is 1. 3) p. use_count (); // The latter returns the number of objects referenced by ptr. Compared with General pointers, shared_ptr can be used to prevent memory leakage and suspension pointers: 1) Memory leakage refers to the forgotten release of the dynamically allocated memory, which prevents this memory from being recycled, it is generally difficult to detect, unless a large amount of leaked memory causes insufficient program memory. If a common pointer is used, this problem may occur, for example, T * p = new T (); the memory block pointed to by p for the first time is leaked. Another situation may also cause memory leakage: T * f (T arg) {T * ptr = new T (arg );...;} // If the ptr does not actively release the memory to which it points, the space to which the ptr points will leak when the function f falls out. A smart pointer is an object. It has its own destructor. When a smart pointer fails, it automatically calls the Destructor and deletes itself, and subtract 1 from the reference value of the object to which the object is directed. If the reference value is 0, the system deletes the object to which the object is directed. 2) The Hanging Pointer Points to a piece of memory space, which previously saves an object, but is deleted later because of the delete other pointers. In this case, the pointer is the hanging pointer. When a smart pointer is used to delete a smart pointer, the reference value pointing to the object is first reduced by 1. If the reference value is 0, the object is deleted. Ptr. reset (); // indicates that ptr no longer points to the object previously pointed. Ptr. reset (q); // indicates that ptr does not point to the object previously pointed to, but to the space corresponding to or directed to q. Ptr. reset (q, d); // same as above. It only replaces the delete function with d. If necessary, it will call d to delete the object previously pointed. Note the following when using smart pointers: 1) do not use different pointers for initialization or reset multiple smart pointers. 2) Do not delete the pointer returned from get. 3) do not use the pointer returned by get () to initialize other smart pointers. 4) if get () is used, it should be noted that when the smart pointer deletes the content of the specified space, the object pointer returned by get () will become a hanging pointer. Unique_ptr <T> ptr is an object that can only point A unique_ptr pointer to. Its initialization methods include: 1) unique_ptr <T> ptr (q ); // q can be a newly allocated memory or a common pointer. 2) unique_ptr <T, D> ptr =...; unlike shared_ptr, unique_ptr must specify a pointer to the custom delete function type. Ptr. release (); // at this time, ptr no longer points to the object previously pointed to, and returns the pointer to the object previously pointed to. It is best to call this function (be sure) store the return value of the function to a pointer, otherwise the memory will leak. Shared_ptr and unique_ptr have the same functions: 1) p; // use p as the condition to determine whether the pointer p is null. 2) * p; // get the object reference pointed to by p. 3) p-> mem; // access the member variable of the object referred to by p. 4) p. get (); // returns a normal pointer to the specified object. Weak_ptr, as its name implies, is a weak pointer. Binding A shared_ptr to weak_ptr does not affect the number of references to the object pointed to by shared_ptr. He mainly has the following methods: 1) weak_ptr <T> w = sp; and w = sp; // bind a javas_ptr to weak_ptr. 2) weak_ptr <T> w (sp); // same as above. 3) reset (); // release the bound sp; 4) use_count (); // reference number of objects. 5) expired (); // determines whether use_count () is 0. 0 returns true; otherwise, false. 6) lock (); // return a bound shared_ptr. If no shared_ptr exists, an empty shared_ptr is returned.