{"id":5063,"date":"2016-12-09T06:46:05","date_gmt":"2016-12-09T06:46:05","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/std-unique-ptr\/"},"modified":"2023-06-26T12:33:16","modified_gmt":"2023-06-26T12:33:16","slug":"std-unique-ptr","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/std-unique-ptr\/","title":{"rendered":"std::unique_ptr"},"content":{"rendered":"<p>According to the RAII idiom, a std::unique_ptr manages automatically and exclusively the lifetime of its resource. <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> should be your first choice because it does its work without memory or performance overhead.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>Before I show you the usage of<span style=\"font-family: courier new,courier;\"> std::unique_ptr,<\/span> I will present its characteristic in a few bullet points.<\/p>\n<p><span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span><\/p>\n<ul>\n<li>can be instantiated with and without resources.<\/li>\n<li>manages the life cycle of a single object but an array of objects.<\/li>\n<li>transparently offers the interface of the underlying resource.<\/li>\n<li>can be parametrized with its deleter function.<\/li>\n<li>can be moved (move semantic).<\/li>\n<li>can be created with the helper function <span style=\"font-family: courier new,courier;\">std::make_unique<\/span>.<\/li>\n<\/ul>\n<h2>The usage<\/h2>\n<p>The critical question of the <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> is when to delete the underlying resource. This happens precisely when the<span style=\"font-family: courier new,courier;\"> std::unique_ptr<\/span> goes out of scope or gets a new resource. Here are the two use cases.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n58\r\n59\r\n60\r\n61<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ uniquePtr.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;memory&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;utility&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">struct<\/span> MyInt{\r\n\r\n  MyInt(<span style=\"color: #2b91af;\">int<\/span> i):i_(i){}\r\n\r\n  ~MyInt(){\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Good bye from \"<\/span> &lt;&lt; i_ &lt;&lt; std::endl;\r\n  }\r\n\r\n  <span style=\"color: #2b91af;\">int<\/span> i_;\r\n\r\n};\r\n\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  std::unique_ptr&lt;MyInt&gt; uniquePtr1{ <span style=\"color: #0000ff;\">new<\/span> MyInt(1998) };\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"uniquePtr1.get(): \"<\/span> &lt;&lt; uniquePtr1.get() &lt;&lt; std::endl;\r\n\r\n  std::unique_ptr&lt;MyInt&gt; uniquePtr2;\r\n  uniquePtr2= std::move(uniquePtr1);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"uniquePtr1.get(): \"<\/span> &lt;&lt; uniquePtr1.get() &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"uniquePtr2.get(): \"<\/span> &lt;&lt; uniquePtr2.get() &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n\r\n  {\r\n    std::unique_ptr&lt;MyInt&gt; localPtr{ <span style=\"color: #0000ff;\">new<\/span> MyInt(2003) };\r\n  }\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  uniquePtr2.reset(<span style=\"color: #0000ff;\">new<\/span> MyInt(2011));\r\n  MyInt* myInt= uniquePtr2.release();\r\n  <span style=\"color: #0000ff;\">delete<\/span> myInt;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  std::unique_ptr&lt;MyInt&gt; uniquePtr3{ <span style=\"color: #0000ff;\">new<\/span> MyInt(2017) };\r\n  std::unique_ptr&lt;MyInt&gt; uniquePtr4{ <span style=\"color: #0000ff;\">new<\/span> MyInt(2022) };\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"uniquePtr3.get(): \"<\/span> &lt;&lt; uniquePtr3.get() &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"uniquePtr4.get(): \"<\/span> &lt;&lt; uniquePtr4.get() &lt;&lt; std::endl;\r\n\r\n  std::swap(uniquePtr3, uniquePtr4);\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"uniquePtr3.get(): \"<\/span> &lt;&lt; uniquePtr3.get() &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"uniquePtr4.get(): \"<\/span> &lt;&lt; uniquePtr4.get() &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class <span style=\"font-family: courier new,courier;\">MyInt<\/span> (lines 7 -17) is a simple wrapper for a number. I have adjusted the destructor in lines 11 &#8211; 13 for observing the life cycle of <span style=\"font-family: courier new,courier;\">MyInt. <\/span><\/p>\n<p>I create in line 24 a <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> and return in line 27 the address of its resource (<span style=\"font-family: courier new,courier;\">new MyInt(1998)<\/span>). Afterward, I move the <span style=\"font-family: courier new,courier;\">uniquePtr1<\/span> to <span style=\"font-family: courier new,courier;\">uniquePtr2<\/span> (line 29). Therefore, <span style=\"font-family: courier new,courier;\">uniquePtr2<\/span> is the owner of the resource. That shows the output of the program in lines 30 and 31. The local <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> in line 37 reaches the end of the scope of its valid range. Therefore, the destructor of the <span style=\"font-family: courier new,courier;\">localPtr<\/span> &#8211; that means the destructor of the resource (new <span style=\"font-family: courier new,courier;\">MyInt(2003))<\/span> &#8211; will be executed. Here is the screenshot.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5059\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtr.png\" alt=\"uniquePtr\" style=\"margin: 15px;\" width=\"414\" height=\"387\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtr.png 414w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtr-300x280.png 300w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><\/p>\n<p>The most interesting lines are lines 42 to 44. At first, I assign the <span style=\"font-family: courier new,courier;\">uniquePtr1<\/span> a new resource. Therefore, the destructor of <span style=\"font-family: courier new,courier;\">MyInt(1998)<\/span> will be executed. After releasing the resource in line 43, I can explicitly invoke the destructor.<\/p>\n<p>The rest of the program is relatively easy to get. I create in lines 48 &#8211; 58 two <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> and swap their resources. <span style=\"font-family: courier new,courier;\">std::swap<\/span> uses under-the-hood move semantic because <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> supports no copy semantic. With the end of the main function, <span style=\"font-family: courier new,courier;\">uniquePtr3<\/span> and <span style=\"font-family: courier new,courier;\">uniquePtr4<\/span> go out of scope, and their destructor will automatically be executed.<\/p>\n<p>That was the big picture. Let&#8217;s dig into a few details of <span style=\"font-family: courier new,courier;\">std::unique_ptr.<\/span><\/p>\n<\/p>\n<h3>Dealing with the lifetime of objects and arrays<\/h3>\n<p><span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> has a specialization for arrays. The access is transparent. That means if the <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> manages the lifetime of an object, the operators for the object access are overloaded (<span style=\"font-family: courier new,courier;\">operator*<\/span> and <span style=\"font-family: courier new,courier;\">operator-&gt;<\/span>); if <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> manages the lifetime of an array, the index operator <span style=\"font-family: courier new,courier;\">operator[]<\/span> is overloaded. The invocations of the operators are, therefore, totally transparent and forwarded to the underlying resource.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ uniquePtrArray.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iomanip&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;memory&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">MyStruct<\/span>{\r\npublic:\r\n  MyStruct(){\r\n    std::cout &lt;&lt; std::setw(15) &lt;&lt; std::left &lt;&lt; (<span style=\"color: #2b91af;\">void<\/span>*) <span style=\"color: #0000ff;\">this<\/span> &lt;&lt; <span style=\"color: #a31515;\">\" Hello \"<\/span>  &lt;&lt; std::endl;\r\n  }\r\n  ~MyStruct(){\r\n    std::cout &lt;&lt; std::setw(15) &lt;&lt; std::left &lt;&lt; (<span style=\"color: #2b91af;\">void<\/span>*)<span style=\"color: #0000ff;\">this<\/span> &lt;&lt; <span style=\"color: #a31515;\">\" Good Bye \"<\/span> &lt;&lt; std::endl;\r\n  }\r\n};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n    \r\n  std::cout &lt;&lt; std::endl;\r\n    \r\n  std::unique_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; uniqInt(<span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>(2011));\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"*uniqInt: \"<\/span> &lt;&lt; *uniqInt &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  {\r\n    std::unique_ptr&lt;MyStruct[]&gt; myUniqueArray{<span style=\"color: #0000ff;\">new<\/span> MyStruct[5]};\r\n  }\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  {\r\n    std::unique_ptr&lt;MyStruct[]&gt; myUniqueArray{<span style=\"color: #0000ff;\">new<\/span> MyStruct[1]};\r\n    MyStruct myStruct;\r\n    myUniqueArray[0]=myStruct;\r\n  }\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  {\r\n    std::unique_ptr&lt;MyStruct[]&gt; myUniqueArray{<span style=\"color: #0000ff;\">new<\/span> MyStruct[1]};\r\n    MyStruct myStruct;\r\n    myStruct= myUniqueArray[0];\r\n  }\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I dereference in line 22 the <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> and get the value of its resource.<\/p>\n<p>MyStruct in lines 7 &#8211; 15 is the base of an array of <span style=\"font-family: courier new,courier;\">std::unique_ptr&#8217;<\/span>s. If I instantiate a <span style=\"font-family: courier new,courier;\">MyStruct<\/span> object, I will get its address. The destructor gives the output. Now it&#8217;s quite easy to observe the life cycle of the objects.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5060\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtrArray.png\" alt=\"uniquePtrArray\" style=\"margin: 15px;\" width=\"434\" height=\"492\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtrArray.png 434w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtrArray-265x300.png 265w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/p>\n<p>I create and destroy in lines 26 &#8211; 28 five instances of <span style=\"font-family: courier new,courier;\">MyStruct.<\/span> Lines 32 &#8211; 36 are more interesting. I create a <span style=\"font-family: courier new,courier;\">MyStruct<\/span> instance on the heap (line 33) and the stack (line 34). Therefore, both objects have addresses from different ranges. Afterward, I assign the local object to the <span style=\"font-family: courier new,courier;\">std::unique_pr<\/span> (line 35). Lines 40 &#8211; 54 follow a similar strategy. Now I assign the local object the first element of <span style=\"font-family: courier new,courier;\">myUniqueArray.<\/span> The index access to the <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> in lines 35 and 43 feels like familiar index access to an array.<\/p>\n<h3>User-supplied deleters<\/h3>\n<p><span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> can have a user-supplied deleter: <span style=\"font-family: courier new,courier;\">std::unique_ptr&lt;int,MyDeleter&gt; uniqPtr(new int(2011), intDeleter).<\/span> The deleter is part of the type. You can use <a href=\"https:\/\/www.modernescpp.com\/index.php\/thread-creation\">callables<\/a> like functions, function objects, or lambda functions. If the deleter has no state, it will not change the size of the <span style=\"font-family: courier new,courier;\">std::unique_ptr.<\/span> If the deleter is a function object with a state or a lambda function that captures its context by value, the <a href=\"https:\/\/www.modernescpp.com\/index.php\/memory-and-performance-overhead-of-smart-pointer\">no-overhead principle <\/a>will not hold anymore. I will write about the deleter in my post about <span style=\"font-family: courier new,courier;\">std::shared_ptr.<\/span><\/p>\n<h3>Replacement for std::auto_ptr<\/h3>\n<p>Classical C++ already has <span style=\"font-family: courier new,courier;\">std::auto_ptr.<\/span> Its job is similar to the job of <span style=\"font-family: courier new,courier;\">std::unique_ptr.<\/span> <span style=\"font-family: courier new,courier;\">std::auto_ptr<\/span> exclusively manages the lifetime of its underlying resource. But <span style=\"font-family: courier new,courier;\">std::auto_ptr<\/span> is very strange. If you copy a <span style=\"font-family: courier new,courier;\">std::auto_ptr,<\/span> its resource will be moved. That means an operation with copy semantic performs under the hood move semantic. That&#8217;s why <span style=\"font-family: courier new,courier;\">std::auto_ptr<\/span> is deprecated, and you should instead use <span style=\"font-family: courier new,courier;\">std::unique_ptr.<\/span> <span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> can only be moved but not copied. You have to invoke std::move on a std::unique_ptr explicitly<span style=\"font-family: courier new,courier;\">.<\/span><\/p>\n<p>The graphic shows the difference between <span style=\"font-family: courier new,courier;\">std::auto_ptr<\/span> and <span style=\"font-family: courier new,courier;\">std::unique_ptr.<\/span><\/p>\n<p>If I execute the following code snippet,<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std::auto_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; auto1(<span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>(5));\r\nstd::auto_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; auto2(auto1);<span>&nbsp;<\/span><\/pre>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5061\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/autoPtrCopy.jpg\" alt=\"autoPtrCopy\" width=\"400\" height=\"174\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/autoPtrCopy.jpg 643w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/autoPtrCopy-300x131.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>the <span style=\"font-family: courier new,courier;\">std::auto_ptr auto1<\/span> will lose its resource. <span style=\"font-family: courier new,courier;\"><\/span><\/p>\n<p><span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> can&#8217;t be copied. Therefore, you have to use move semantics.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std::unique_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; uniqueo1(<span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>(5));\r\nstd::unique_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; unique2(std::move(unique1));\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5062\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtrCopy.jpg\" alt=\"uniquePtrCopy\" width=\"400\" height=\"159\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtrCopy.jpg 643w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/12\/uniquePtrCopy-300x119.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p><span style=\"font-family: courier new,courier;\">std::unique_ptr<\/span> can be moved into the containers of the STL and afterward be used in the algorithm of the STL if they do not use copy semantics internally.<\/p>\n<p>To be precise. The copying of a <span style=\"font-family: courier new,courier;\">std::auto_ptr<\/span> is undefined behavior. The moving of <span style=\"font-family: courier new,courier;\">std::unqiue_ptr<\/span> puts the source in a well-defined but not exactly specified state. But the depicted behavior is quite likely.<\/p>\n<h3>The helper function std::make_unique<\/h3>\n<p>In C++11, we have <span style=\"font-family: courier new,courier;\">std::make_shared<\/span> but not <span style=\"font-family: courier new,courier;\">std::make_unique.<\/span> This is fixed with C++14. Although Microsoft Visual Studio 2015 officially supports C++11, you can use <span style=\"font-family: courier new,courier;\">std::make_unique.<\/span> Thanks to <span style=\"font-family: courier new,courier;\">std::make_unique,<\/span> you do not have to touch new.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">std::unique_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; uniqPtr1= std::make_unique&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(2011);\r\n<span style=\"color: #0000ff;\">auto<\/span> uniqPtr2= std::make_unique&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(2014);\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If you use <span style=\"font-family: courier new,courier;\">std::make_unique<\/span> in combination with automatic type deduction, your typing is reduced to its bare minimum. That proves <span style=\"font-family: courier new,courier;\">std::unique_ptr uniqPtr2<\/span>.<\/p>\n<h4>Always use std::make_unique<\/h4>\n<p>There is another subtle reason to use<span style=\"font-family: courier new,courier;\"> std::make_unique. std::make_unique&nbsp;<\/span>is always correct.<\/p>\n<p>If you use<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\">func(std::make_unique&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(2014), functionMayThrow());\r\nfunc(std::unique_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(<span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>(2011)), functionMayThrow());\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>and <span style=\"font-family: courier new,courier;\">functionMayThrow<\/span> throws, you have a memory leak with <span style=\"font-family: courier new,courier;\">new int(2011) <\/span>for this possible sequence of calls:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">new<\/span> <span style=\"color: #2b91af;\">int<\/span>(2011)\r\nfunctionMayThrow()\r\nstd::unique_ptr&lt;<span style=\"color: #2b91af;\">int<\/span>&gt;(...)\r\n<\/pre>\n<\/div>\n<h2>What&#8217;s next?<\/h2>\n<p>The next post is about<span style=\"font-family: courier new,courier;\"> std::shared_ptr.<\/span> Therefore, this post was about exclusive ownership, and the <a href=\"https:\/\/www.modernescpp.com\/index.php\/std-shared-ptr\">next post <\/a>will be about shared ownership.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>According to the RAII idiom, a std::unique_ptr manages automatically and exclusively the lifetime of its resource. std::unique_ptr should be your first choice because it does its work without memory or performance overhead.<\/p>\n","protected":false},"author":21,"featured_media":5059,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[498,399,401],"class_list":["post-5063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-memory","tag-smart-pointers","tag-unique_ptr"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5063"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5063\/revisions"}],"predecessor-version":[{"id":6916,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5063\/revisions\/6916"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5059"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}