Item 13:use objects to manage resources.
People who are familiar with smart pointers are certainly not unfamiliar with this. Automatically frees resources by using the features of object Auto-destruction in C + +. The C + + compiler does not provide an automatic garbage collection mechanism, so the responsibility for freeing resources falls on the developer's head. We are required to always use new
and delete
, for example:
investment * pinv = createinvestment (); ... delete pinv
createinvestment
method belongs to the factory method (Factory function), which is used to create an instance of an object.
The above code does perform well without leaking memory, but the problem is createinvestment ()
function gives the customer the responsibility to release the resource, but does not explicitly declare it, so the customer is sometimes unaware. Even if the customer knows that the resource needs to be destroyed, the resource may not be released in a timely manner due to flow control statements or exceptions.
Fortunately, we can use objects to wrap resources and release them in destructors. This way the customer does not need to maintain the memory of the resource. std::auto_ptr
is such an object, which is called smart pointer (smart pointer). A typical usage scenario is that the resource is stored in the heap space but is only partially used.
void f () { std :: auto_ptr < investment > pinv ( createinvestment ()); }
For the use of heap space and stack space in C + +, refer to: Process address space: Text,data,bss,heap,stack
Inf()
At the end of the callpInv
Exits the scope, the destructor is called, and finally the resource is freed. In fact, letcreateInvestment
Direct return of smart pointers is a better design. As you can see, the key to using objects to manage resources is:immediately after the resource is created, put in the resource management object and use the destructor of the resource management object to ensure that the resource is freed。
The implementation framework for resource Management Objects is RAII principle: acquisition is initialization, which initializes a smart pointer with a resource. The resource is freed from the destructor of the pointer.
It should be noted that in order to prevent the object from being released more than once, auto_ptr
it should be non-replicable. copying one auto_ptr
will make it empty , and the resource is delivered to the other only pointer.
STD::auto_ptr<int> P1 (New int);*P1.Get()=Ten;STD::auto_ptr<int> P2 (P1);STD::cout << "P2 points to" << *P2 << ' \ n ';//P2 points to ten//(P1 is now null-pointer auto_ptr)
.get
method returns a pointer to a resource.
auto_ptr
Bizarre replication behavior makes it not the best way to manage resources, even in STLauto_ptr
Container is not allowed: You can create such a container, but add elements to it (for example,push_back
) causes a compilation error.
auto_ptr<int> P1(New int);Vector<auto_ptr<int>> v; //OK, can compilev.push_back(P1); //Compile wrong!
Here we introduce a pointer to a reference count (reference-counting smart POINTER,RCSP) shared_ptr
. It frees resources when no other pointer is referenced to the resource. Unlike the garbage collector, shared_ptr
the issue of circular references could not be resolved.
It is important to note thatauto_ptr
Andshared_ptr
Only individual resources can be managed, because they are useddelete
Rather thandelete[]
To achieve the release of resources. A common mistake is to pass an array in:
std :: tr1 :: shared_ PTR < int > spi ( new int [ 1024 );
In the latest C + + standard, smart pointers are already grouped into std
namespaces. We can use it this way: std::shared_ptr<int>
.
Although smart pointers have this problem, C + + does not provide a smart pointer to manage arrays becausevector
And so the container can do the job well. If you really need to, you can ask for help with the boost communityboost::scoped_array
Andboost::shared_array
。
Unless noted, this blog article is original, reproduced please link to the form of this article address: http://harttle.com/2015/08/02/effective-cpp-13.html
Copyright NOTICE: This article is for bloggers original articles, reproduced please attach the original link.
Item 13: Using Objects (smart pointers) to manage resources effective C + + notes