For a C + + user. The use of pointers can be counted as the norm, but in the process of use. Much of the time may be due to new or malloc objects, the last forgotten release ends (I will make such a mistake). Memory leaks.
At this point the smart pointer may help me to solve the problem.
Smart pointers (smart pointer) use a policy of reference counting to handle the release of pointers, thus guaranteeing the security of pointers.
In general, we will design a smart pointer class to manage our own pointer objects.
In fact, the principle is to store pointer object classes that point to dynamic allocations. By the function of reference counting to control, to properly implement the destruction of the pointer object, so as to avoid memory leaks.
The principle of smart pointers is. By associating a pointer class with a reference count, the reference count is calculated and the current pointer is shared by how many objects.
Each time a new pointer object is created, the pointer is initialized and the reference count is set to 1, when the object is created as a copy of the object. Copy the constructor copy the pointer and add the corresponding reference count. When an object is assigned a value. The assignment operator reduces the reference count of the object referred to by the left operand (the object is deleted if the reference count is reduced to 0), and adds a reference count for the object referred to by the right operand; when the destructor is called. The constructor reduces the reference count (assuming that the reference count is reduced to 0, deleting the underlying object).
The following code is mainly borrowed from the Baidu Encyclopedia code to learn smart pointers:
#include <iostream> #include <stdexcept>using namespace std; #define Test_smartptrclass stub{public:void Print () {cout<< "Stub:print" <<ENDL;} ~stub () {cout<< "Stub:destructor" <<endl;}}; Template<typename t>class smartptr{public:smartptr (t*p=0):p TR (P), Puse (New size_t (1)) {}smartptr (const smartptr& src):p tr (src.ptr), Puse (src.puse) {++*puse;} smartptr&operator= (const smartptr& RHS) {//self-assigningisalsoright++*rhs.puse;decruse ();p tr=rhs.ptr; Puse=rhs.puse;return *this;} t* operator-> () {if (PTR) return ptr;throw std::runtime_error ("Accessthroughnullpointer");} Const t* operator-> () const{if (PTR) return ptr;throw std::runtime_error ("Accessthroughnullpointer");} T &operator* () {if (PTR) return *ptr;throw std::runtime_error ("Dereferenceofnullpointer");} Const T &operator* () const{if (PTR) return *ptr;throw std::runtime_error ("Dereferenceofnullpointer");} ~smartptr () {decruse (); #ifdef test_smartptrstd::cout<< "Smartptr:destructor" <<std::endl;//Fortesting#endif}private:void Decruse () {if (--*puse==0) {delete Ptr;delete puse;}} t* ptr;size_t* puse;}; int main () {try{smartptr<stub> t;t->print ();} catch (const Exception&err) {cout<<err.what () <<endl;} SMARTPTR<STUB>T1 (new Stub); SMARTPTR<STUB>T2 (t1); SMARTPTR<STUB>T3 (new Stub); T3=t2;t1->print ();(*t3). print (); return 0;}In the code of the polygon, the smart pointer is generally overloaded with the-and * operator, so that it behaves as a pointer, and you can use it as if you were using a pointer.
At the time of the function destruction, we will find the function of reference counting in the middle, so as to avoid the early release of the pointer by mistake. Causes the pointer to hang. or release not completely, causing a memory leak problem.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
C + + in the design and use of smart pointers