Smart_ptr
- RAII (Resource acquisition is initialization)
- The smart pointer series are collectively referred to as SMART_PTR, including the C++98 standard auto_ptr
- A smart pointer is a class that completes operations like the original pointer, by overloading and *. But because it's a class, you can do things like memory management, thread safety, and so on.
- Smart pointers are automatically managed memory and do not need to display call Delete
Scoped_ptr
- The biggest difference from auto_ptr is the privatization of structure and copy construction, so that the operation is not transferable, so there is a strong scope attribute, and the pointer class itself is responsible for releasing
- Compared to the C++11 standard UNIQUE_PTR: Unique_ptr has more features that can be compared as a raw pointer, custom-removed like a shared_ptr, or safely placed in a standard container. But less is more, SCOPE_PTR focuses on the emphasis on the scope properties.
Scoped_array
- Similar to scoped_ptr, except that the encapsulated new[] allocation array.
- is not a pointer, there is no overload to implement * and, but direct [] subscript access
- Scoped_array is often not recommended, and its appearance often means that there is a hidden danger in your code.
shared_ptr (Key)
- The most valuable and important part of BOOST.SMART_PTR, and the most frequently used
- Unique () returns True when Shared_ptr is the only owner in a pointer
- User_count () returns the reference count of the current pointer
- Provides operator<, = = comparison operations so that shared_ptr can be used for SET/MAP standard containers
- When you write a polymorphic pointer, you cannot use a form such as static_cast< t* > (P.get ()), which causes the transformed pointer to no longer be properly managed by shared_ptr. To support this usage, SHARED_PTR provides built-in transformation functions static_pointer_cast< T > (), const_pointer_cast< T > (), and Dynamic_pointer_cast < T > ()
- Support operator< < Manipulate print pointer values, and debug
- There are factory functions
TT... args);
shared_ptr<string> SPS (New string("Smart"); Assert (sps->size () = =5);shared_ptr<string> SP = make_shared<string> ("Make_shared");shared_ptr< vector< int >> SPV = make_shared< vector< int >> (Ten,2); Assert (spv->size () = =Ten);
- Apply to standard container code cases:
#include < boost/make_shared.hpp>intMain () {typedef vector< shared_ptr< int>> vs; VS V (Ten);intI=0; for(Vs::iterator pos = V.begin (); pos! = V.end (); ++pos) {(*pos) = make_shared<int> (++i);cout<< * (*pos) <<","; }cout<< Endl;shared_ptr<int> P = v[9]; *p = -;cout<< *v[9] << Endl;}
Shared_array
- Usually replaced with shared_ptr< std::vector> or std::vector< shared_ptr>
WEAK_PTR (Key)
- Not overloaded * and->, the biggest role is to assist shared_ptr, observing the use of resources
- code example:
intTestweakptr () {boost::shared_ptr<int> Sp (New int(Ten));assert(Sp.use_count () = =1); boost::weak_ptr<int> wp (SP);assert(Wp.use_count () = =1);if(!wp.expired ()) {boost::shared_ptr<int> SP2 = Wp.lock (); *SP2 = -;assert(Sp2.use_count () = =2);assert(Wp.use_count () = =2); }assert(Wp.use_count () = =1); Sp.reset ();assert(wp.expired ());assert(!wp.lock ());return 0;}
- The function is to break the circular reference, code example:
//The case of circular referencesclassnode{ Public: Boost::shared_ptr<node> Next; ~node () {cout <<"Delete Node"<< Endl; }};intTestweakptrrecyclewrong () {AutoN1 = boost::make_shared<node> ();AutoN2 = Boost::make_shared<node> (); N1->next = n2; N2->next = N1;assert(N1.use_count () = =2);assert(N2.use_count () = =2);return 0;//circular reference, destructor exception, program exit still not destructor}//Avoid circular references, mainly the shared_ptr of node is replaced by weak_ptrclassnode1{ Public: Boost::weak_ptr<node1> Next; ~node1 () {cout <<"Delete Node1"<< Endl; }};intTestweakptrrecycleright () {AutoN1 = boost::make_shared<node1> ();AutoN2 = Boost::make_shared<node1> (); N1->next = n2; N2->next = N1;assert(N1.use_count () = =1);assert(N2.use_count () = =1);if(!n1->next.expired ()) {//Call Lock () to get a strong reference, Count plus 1 AutoN3 = N1->next.lock ();assert(N2.use_count () = =2); }assert(N2.use_count () = =1);return 0;}
Intrusive_ptr (slightly) pool (the pool suffix is the focus)
- Pool manages memory, only for the underlying type pod (Plain old Data), because the object's constructor is not called. You don't need to release yourself unless you have special circumstances.
- To invoke the sample code:
int testPool(){ pool<> pl(sizeof(int)); intstatic_cast<int*>(pl.malloc// void*->int* assert(pl.is_from(p)); pl.free(p); for (int0100;i++) { pl.ordered_malloc(10); } return0;}
Object_pool
- A subclass of pool that implements a pool of memory for a class instance (object) that invokes the destructor to properly release the resource
- Example code:
structdemo_class{ Public:intA, B, C; Demo_class (intx =1,inty =2,intz =3): A (x), B (Y), C (z) {} ~demo_class () {cout <<"Destruct"<< Endl; }};intTestobjpool () {object_pool<demo_class> PL; Demo_class *p = Pl.malloc ();assert(Pl.is_from (P));//malloc was not initialized when it was created assert(p->a!=1|| P->b! =2|| P->c! =3); p = pl.construct ();//Boost comes with construct can only support 3 or less parameter callsp = pl.construct (7,8,9);assert(P->a = =7); object_pool<string> pls; for(inti =0; I <Ten; i++) {string*ps = Pls.construct ("Hello Object_pool"); cout << *ps << Endl; }return 0;}
Destructors are called three times, and malloc and two construct require a call to destructor. It is object_pool to determine the scope of the automatic adjustment.
Singleton_pool
- Basic type static single piece (including underlying pointer type), providing thread safety
- Sample code
struct pool_tag{intTag;}; typedef singleton_pool<Pool_tag, sizeof (Pool_tag*)>Spl;int Testsingletonpool () {Pool_tag**P=(Pool_tag**) SPL:: malloc(); ASSERT (SPL:: Is_from(p)); Pool_tag PTag; PTag.Tag = 3;*P= &PTag; cout<< "Testsingletonpool:" <<(*P -Tag <<Endl Spl:: Release_memory();return 0;}
Pool_alloc
- Provides a memory allocator for a standard container type that can throw exceptions when allocation fails. But unless specifically required, the STL should be used. If you want to use the pool_alloc need to undergo careful testing, to ensure that the container can work properly.
Boost Memory management