<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: Single Ownership in C++ (intro to auto_ptr)</title>
    <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Single Ownership in C++ (intro to auto_ptr)</title>
      <description>&lt;p&gt;I find a lot of C++ programmers experience memory management differently than I do.  I used to have the same kinds of problems that they have, but I learned an important rule:  &lt;strong&gt;every heap object has exactly one owner, and &lt;i&gt;n&lt;/i&gt; borrowers.&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;The way that I realize this is using reference types.  Of course, all good  C++ programmers write their functions to use the canonical &amp;#8220;const T&amp;#38;&amp;#8221; parameters whenever possible.  Using a reference means that we have an actual object and not a null reference (side effect: no null checks) and we have simple dot notation (side effect: less typing), but I also use this to mean that we are borrowing the object, not owning it.   Likewise, if we have to drop the &amp;#8216;const&amp;#8217;, we are still borrowing.&lt;/p&gt;


What about passing by pointer? I only do that if I absolutely must accept 0 (null) as a possible value.  I always hate to do that.  I would rather have an object outright than a pointer that I dereference.  If the function is being called by a framework, I don&amp;#8217;t get a choice.  I might do something like this:
&lt;pre&gt;
     void functionName(const Snorkle* snorkleWithNull) {
             if (0 == snorkleWithNull) { 
                  throw SomeIdiotPassedMeANull();
            const Snorkle&amp;#38; snorkle = snorkleWithNull;

           // ... work with snorkle, ignore snorkleWithNull ... 

     }
&lt;/pre&gt;
However sick that seems (and it does) it reverts back to the const reference case quickly and I&amp;#8217;m left with a reference to a real object.   It keeps me from forgetting to dereference when I&amp;#8217;m in a hurry, and keeps me from casting.  I have to work extra-hard to do bad things, and that&amp;#8217;s a good thing.  On the other hand, in modern &lt;span class="caps"&gt;TDD&lt;/span&gt; style, with refactoring, it can double the length of a function (hence feeling sick).

	&lt;p&gt;Either way, if you are a borrower, you have no right to delete the object.  You can reference it for the duration of your function, or (if the intent is that you will have a short-lived object) for the duration of the object lifetime, but no ownership is given by pointer or reference.  Remember that we still have a risk of the pointer being dangled, so holding a reference/pointer for longer than a method call should be considered fairly dangerous and must be done rather intentionally.&lt;/p&gt;


	&lt;p&gt;But what about transfer of ownership? That&amp;#8217;s what the auto_ptr in &amp;lt;memory&amp;gt; is for.   Yes, it&amp;#8217;s a template.  If I can&amp;#8217;t use templates, I&amp;#8217;ll make up something new that acts roughly the same.    The auto_ptr is the one structure in C++ that undeniably says &amp;#8220;owner&amp;#8221;.  When I receive an object via auto_ptr, I am now the owner.  When I return an auto_ptr from a function, I&amp;#8217;ve clearly given up ownership.&lt;/p&gt;


	&lt;p&gt;The recipient of the auto_ptr has to do something with the referenced object, or else it will be deleted when the auto_ptr passes out of scope.  If the recipient decided to &lt;strong&gt;release&lt;/strong&gt; the auto_ptr, they can keep a normal pointer or reference in whatever structure pleases them.  They can make a copy and delete the original (remember the warning about dangling).&lt;/p&gt;


	&lt;p&gt;The trick is to remember that 99% of the time, you want to be a borrower, and that any class that is going to hold a borrower&amp;#8217;s reference to an object needs an algorithmic guarantee that the referenced object will outlive the reference.&lt;/p&gt;


	&lt;p&gt;The scary part is for an object to have a large number of long-term borrowers, because there is the constant fear that one might outlive the owner.  In that case, you need to hang the auto_ptr on the shelf and go get some bigger magic.  You might actually need the smart pointer.    I have created reference-counting pointer templates in several projects, which  will cause C++ to act more like a garbage-collecting language. I know that there are 3rd-party libraries for garbage-collecting.  If I find myself in a situation where there are a number of long-term borrowers, I will definitely move in one of these directions again.  It is too important to leave object ownership up to chance or naming convention.&lt;/p&gt;</description>
      <pubDate>Fri, 30 Mar 2007 10:48:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:af181d4b-afe1-43ce-9287-6646801e9745</guid>
      <author>Tim Ottinger</author>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr</link>
      <category>Tim's Tepid Torrent</category>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by Fred</title>
      <description>&lt;p&gt;Plug for &lt;a href="http://boost.org/libs/smart_ptr/smart_ptr.htm" rel="nofollow"&gt;Boost smart pointers&lt;/a&gt;. When I&amp;#8217;m on a project where I can use Boost, I never allow naked pointers.&lt;/p&gt;</description>
      <pubDate>Fri, 27 Apr 2007 07:14:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:d3e45bb5-fd56-44fd-b510-ed1511b6c08f</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-188</link>
    </item>
  </channel>
</rss>
