1. Programmer's Explicit memory release
For C + + programmers, the most mind is to manage the dynamically allocated memory. C + + allocates memory on the heap, requiring the programmer to dispose of the allocated memory. But sometimes the release of memory doesn't seem like a very easy thing to do, the following program
void func(){ intnewint(0); if(一些判断) { return; } newint(1); delete p;}
This function has no meaning, just to illustrate the problem. The Func function has at least three problems. 1. Once the judgment condition of the IF is established, the return statement is executed immediately. The memory that P points to will not be released at this point (the novice will usually make a mistake). 2. p = new int (1), the statement does not release memory that was originally allocated on the heap, causing the original memory to never be released until the end of the program. 3. The problem is not so obvious, assuming the code for the Func () function is very long.
void func(){ int *p = new int(0); ... delete p;}
There is a lot of code between int *p = new int (0) and delete p, and this middle code may have code that has already executed delete p, and has not placed P 0. P is the dangling pointer at this point. Subsequent execution of the delete p will result in a run-time error.
2. A smart pointer management class similar to boost shared_ptr
Here is a smart pointer management class that references C + + primer and effective C + + itself. With Smartptr, you no longer need to be concerned about the release of memory.
#ifndef Smartptr_include_h#define SMARTPTR_INCLUDE_H#include <iostream>using namespace STD;namespacecommutil{Template<TypeNameT>classSmartptr { Public: Smartptr (T *p =0); Smartptr (ConstSmartptr &); Smartptr &operator=(ConstSmartptr &); ~smartptr (); T &operator*(); S Moperator();Private:voidDecreaseref (); T *m_p;int*m_usecnt; };Template<TypeNameT> smartptr<t>::smartptr (T *p=0): M_p (P) {//When this constructor is called, the default number of references is 1m_usecnt =New int(1); }Template<TypeNameT> Smartptr<t>::smartptr (ConstSmartptr &rhs) {//Use the copy constructor to create a new object with a reference number plus 1 This->m_p = rhs.m_p; This->m_usecnt = rhs.m_usecnt; (*m_usecnt) + +; }Template<TypeNameT> smartptr<t>::~smartptr () {decreaseref (); }Template<TypeNameT>voidSmartptr<t>::d ecreaseref () {if(--(*m_usecnt) = =0) {//When the reference count is 0 o'clock, the heap memory is freed Deletem_usecnt;Deletem_p; } }Template<TypeNameT> T &smartptr<t>::operator*() {//Overloaded dereference return*m_p; }Template<TypeNameT> T *smartptr<t>::operator() {//overloading operator, returns the real object pointer returnm_p; }Template<TypeNameT> smartptr<t> &smartptr<t>::operator=(ConstSmartptr &anotherptr) {if( This==&ANOTHERPTR) {//Prevent self-assignment return* This; }//using the assignment operator, the number of references to the originally referred object is reduced by 1. Decreaseref (); m_p = anotherptr.m_p; m_usecnt = anotherptr.m_usecnt; ++*m_usecnt;//points to the new object, referring to the number of objects plus 1 return* This; }}#endif
3. Case studies using SMARTPTR 3.1 example of the first smartptr
void func(){ SmartPtr<int> autoPtr(newint(1));}
In Func, a Smartptr object autoptr is defined, and a new int (1) is allocated on the heap to allocate a piece of memory to the constructor of Smartptr for the first address of the allocated memory. At this point, the value of Autoptr m_usecnt is 1. When the Func execution completes, the Autoptr object is out of its scope and calls the Autoptr destructor. Call the private Decreaseref () function in the destructor to reduce the memory value of Autoptr m_usecnt in Decreaseref () by 1, when m_usecnt refers to a memory value of 0, performing a delete m_usecnt;delete m_p ;。 The dynamically allocated memory is freed when the Autoptr object is created.
3.2 Smartptr with a return statement
void func(){ SmartPtr<int> autoPtr(newint(1)); if(判断条件) { return; }}
The If judgment condition in the Func () function is set to execute return; After the statement, the autoptr exceeds its scope and memory is freed. See 3.1 for analysis
3.3 Smartptr object to manage new Smartptr objects
SmartPtr<int> autoPtr1(newint(0)); SmartPtr<int> autoPtr2(newint(1)); autoPtr1 = autoPtr2;
Execution autoPtr1 = AUTOPTR2, because the Smartptr class overloads the assignment operator. The actual execution is Smartptr's operator= () function, in the above assignment statement, the first autoPtr1 of the m_usecnt refers to the memory value minus 1. At this point the memory value of M_USECNT is changed to 0, and the memory released by M_p is performed. The second problem mentioned in 1 was solved using SMARTPTR. And all memory management and release work is done by smartptr, and programmers don't need to worry about when to release memory.
3.4SmartPtr Shared Object Resources
Unlike Auto_ptr, Smartptr uses a reference counting mechanism to ensure that multiple Smartptr objects can manage the same object resource
void func(){ SmartPtr<int> autoPtr1(newint(0)); SmartPtr<int> autoPtr2(autoPtr1);}
Execute smartptr autoPtr2 (AUTOPTR1) Call the copy constructor of Smartptr, and in the copy constructor, point autoPtr2 m_usecnt and m_p respectively to the memory pointed to by AUTOPTR1, and then m_ USECNT the number of references plus 1. At the end of the Func () function, two calls to Smartptr's destructor, two destructors after the reference number of 0, delete m_usecnt,m_p referred to as memory.
3.5SMARTPTR Access class member functions
class A{public: voidprint() { cout<<"A"<<endl; }};void func(){ new A(); obj->print(); delete obj;}
The smartptr operator is overloaded, and you can access member functions like pointers to smartptr objects.
func(){ SmartPtr<AautoPtr(new A()); autoPtr->print();}
4. Summary
The Smartptr class implements "self-management" of memory objects, and using SMARTPTR programmers no longer need to be concerned about the release of dynamic memory. Unlike shared_ptr, the Smartptr class can only manage resources for pointer types, and SMARTPTR does not support custom resource deallocation functions.
C + + Smart pointer Management class