Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Smart pointer
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== shared_ptr and weak_ptr == C++11 introduces {{code|std::shared_ptr}} and {{code|std::weak_ptr}}, defined in the header {{code|<memory>}}.<ref name="ISO14882_2011"/> C++11 also introduces {{Code|std::make_shared}} ({{Code|std::make_unique}} was introduced in C++14) to safely allocate dynamic memory in the [[Resource acquisition is initialization|RAII]] paradigm.<ref name="ISO14882_2014"/> A {{code|shared_ptr}} is a container for a [[raw pointer]]. It maintains [[reference counting]] ownership of its contained pointer in cooperation with all copies of the {{code|shared_ptr}}. An object referenced by the contained raw pointer will be destroyed when and only when all copies of the {{code|shared_ptr}} have been destroyed. <syntaxhighlight lang="cpp"> std::shared_ptr<int> p0(new int(5)); // Valid, allocates 1 integer and initialize it with value 5. std::shared_ptr<int[]> p1(new int[5]); // Valid, allocates 5 integers. std::shared_ptr<int[]> p2 = p1; // Both now own the memory. p1.reset(); // Memory still exists, due to p2. p2.reset(); // Frees the memory, since no one else owns the memory. </syntaxhighlight> A {{code|weak_ptr}} is a container for a raw pointer. It is created as a copy of a <code>shared_ptr</code>. The existence or destruction of {{code|weak_ptr}} copies of a <code>shared_ptr</code> have no effect on the <code>shared_ptr</code> or its other copies. After all copies of a <code>shared_ptr</code> have been destroyed, all <code>weak_ptr</code> copies become empty. <syntaxhighlight lang="cpp"> std::shared_ptr<int> p1 = std::make_shared<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. // p2 is initialized from a weak pointer, so you have to check if the // memory still exists! if (p2) { DoSomethingWith(p2); } } // p2 is destroyed. Memory is owned by p1. p1.reset(); // Free the memory. std::shared_ptr<int> p3 = wp1.lock(); // Memory is gone, so we get an empty shared_ptr. if (p3) { // code will not execute ActionThatNeedsALivePointer(p3); } </syntaxhighlight> Because the implementation of {{code|shared_ptr}} uses [[reference counting]], [[Reference count#Dealing with reference cycles|circular references]] are potentially a problem. A circular <code>shared_ptr</code> chain can be broken by changing the code so that one of the references is a <code>weak_ptr</code>. Multiple threads can safely simultaneously access different {{code|shared_ptr}} and {{code|weak_ptr}} objects that point to the same object.<ref name="Boost"/> The referenced object must be protected separately to ensure [[thread safety]]. {{code|shared_ptr}} and {{code|weak_ptr}} are based on versions used by the [[Boost (C++ libraries)|Boost libraries]].{{citation needed|date=January 2014}} [[C++ Technical Report 1]] (TR1) first introduced them to the standard, as [[C++ Technical Report 1#General utilities|general utilities]], but C++11 adds more functions, in line with the Boost version.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)