Smart pointerFrom Wikipedia, the free encyclopedia
In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the misuse of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management. They may also be used to manage other resources, such as network connections and file handles. The misuse of pointers is a major source of bugs: the constant allocation, deallocation and referencing that must be performed by a program written using pointers introduces the risk that memory leaks will occur. Smart pointers try to prevent memory leaks by making the resource deallocation automatic: when the pointer (or the last in a series of pointers) to an object is destroyed, for example because it goes out of scope, the pointed object is destroyed too. Several types of smart pointers exist. Some work with reference counting, others by assigning ownership of the object to a single pointer. If the language supports automatic garbage collection (for instance, Java or C#), then smart pointers are unnecessary for memory management, but may still be useful in managing other resources.
[edit] C++ Smart PointersIn C++, smart pointers may be implemented as a template class that mimics, by means of operator overloading, the behaviour of traditional (raw) pointers, (e.g.: dereferencing, assignment) while providing additional memory management algorithms. Smart pointers can facilitate intentional programming by expressing the use of a pointer in the type itself. For example, if a C++ function returns a pointer, there is no way to know whether the caller should delete the memory pointed to when the caller is finished with the information. some_type* ambiguous_function(); // What should be done with the result? Traditionally, this has been solved with comments, but this can be error-prone. By returning an auto_ptr<some_type> obvious_function1(); the function makes explicit that the caller will take ownership of the result, and furthermore, that if the caller does nothing, no memory will be leaked. [edit] unique_ptrC++11 provides The copy constructor and assignment operators of C++11 provides support for move semantics; it allows for the explicit
support of transferring values as a different operation from copying
them. C++11 also provided support for explicitly preventing an object
from being copied. Since This pointer type has its copy constructor and assignment operator
explicitly deleted; it cannot be copied. It can be moved using std::unique_ptr<int> p1(new int(5)); std::unique_ptr<int> p2 = p1; //Compile error. std::unique_ptr<int> p3 = std::move(p1); //Transfers ownership. p3 now owns the memory and p1 is rendered invalid. p3.reset(); //Deletes the memory. p1.reset(); //Does nothing.
[edit]C++11 incorporates
std::shared_ptr<int> p1(new int(5)); std::shared_ptr<int> p2 = p1; //Both now own the memory. p1.reset(); //Memory still exists, due to p2. p2.reset(); //Deletes the memory, since no one else owns the memory. A std::shared_ptr<int> p1(new int(5)); std::weak_ptr<int> wp1 = p1; //p1 owns the memory. { std::shared_ptr<int> p2 = wp1.lock(); //Now p1 and p2 own the memory. if(p2) //Always check to see if the memory still exists { //Do something with p2 } } //p2 is destroyed. Memory is owned by p1. p1.reset(); //Memory is deleted. std::shared_ptr<int> p3 = wp1.lock(); //Memory is gone, so we get an empty shared_ptr. if(p3) { //Will not execute this. } [edit] Concurrency issuesOperations that change the reference count, due to copying or destroying The above only applies when multiple threads have their own [edit] See also
[edit] References[edit] External links
|
|
来自: WUCANADA > 《c plus plus》