<?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 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:af181d4b-afe1-43ce-9287-6646801e9745</guid>
      <author>tottinger</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 ysbearing</title>
      <description>&lt;p&gt;Slewing ring is also called slewing bearing, some people called: rotary support, swing support. English Name: slewing bearing or slewing ring bearing or turn table bearing, slewing ring in the real industrial applications is very wide.&lt;/p&gt;</description>
      <pubDate>Wed, 19 Oct 2011 04:31:19 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:21c70f9d-2e34-4847-a1f0-e9e3dac5ee91</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-159673</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by balmain dresses</title>
      <description>&lt;p&gt;thanks for share with us&lt;/p&gt;</description>
      <pubDate>Wed, 14 Sep 2011 21:23:54 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:0e963b46-e180-411b-942f-993f45fa2ce3</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-140376</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by bagsupplyer</title>
      <description>&lt;p&gt;Thank you very much.Waiting for update.
&lt;a href="http://www.bagsupplyer.com/G-Star-n823/" rel="nofollow"&gt;Fashion designer Men G-star sweaters from China for wholesale at on line store&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Sat, 10 Sep 2011 22:49:47 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5aacb435-2716-480e-9406-362538351189</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-139148</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by beats by dre store</title>
      <description>&lt;p&gt;to vote to thWealthy people: &amp;#8220;You suck one, do not look for friends!&amp;#8221; 
1, Pol.ice: drink?&lt;a href="http://www.drdrebeatsheadphones-australia.com" rel="nofollow"&gt;beats by dre sale&lt;/a&gt;
&lt;a href="http://www.drdrebeatsheadphones-australia.com" rel="nofollow"&gt;cheap beats by dre&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 23 Aug 2011 02:49:59 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:fcd3397f-d697-4fed-a03c-90225c9d4a26</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-131625</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by beats by dr dre headphones</title>
      <description>&lt;p&gt;These &lt;a href="http://www.drebeatsstudio.com/beats-by-dr-dre-solo-c-5.html" rel="nofollow"&gt;beats by dr dre solo&lt;/a&gt; are just the same as selling in the franchise store.Or even better.&lt;/p&gt;</description>
      <pubDate>Fri, 03 Jun 2011 23:43:23 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ccbd0f8a-9663-4e8e-9391-6948ae66c59c</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-107891</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by christian louboutin shoes on sale</title>
      <description>&lt;p&gt;Have the &lt;a href="http://www.blacklouboutinshoes.com/pumps-c-2.html" rel="nofollow"&gt;christian louboutin patent leather pumps&lt;/a&gt;  is a happy thing. 
Here have the most complete kinds of  &lt;a href="http://www.blacklouboutinshoes.com/platforms-c-3.html" rel="nofollow"&gt;christian louboutin leather platform pumps&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Fri, 03 Jun 2011 07:45:35 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:721ae12f-dae8-4f67-9f8d-c6e7ecee88ad</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-107700</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by okey oyunu oyna </title>
      <description>&lt;p&gt;Thanks so much  it is nice article.&lt;/p&gt;


	&lt;p&gt;Okey oynamak hi&#231; bu kadar zevkli olmadi. Online ve 3 boyutlu &lt;a href="http://www.okeyoyunu-oyna.com" rel="nofollow"&gt;okey oyunu oyna&lt;/a&gt; ve turnuvalara sende katil.&lt;/p&gt;</description>
      <pubDate>Mon, 25 Apr 2011 10:37:35 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e85fc666-73b4-45b3-86cb-6c66c214b31a</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-90530</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by christian louboutin sales</title>
      <description>&lt;p&gt;Awesome idea for a tutorial list. Am definitely adding to mems to browse as and when.&lt;/p&gt;</description>
      <pubDate>Mon, 18 Apr 2011 02:21:09 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5a0d0350-aa57-49cf-9809-6606cdc64421</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-86390</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by chanel handbags for sale</title>
      <description>&lt;p&gt;Very comprehensive, and definitely helpful. &lt;strong&gt;is awed&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 18 Apr 2011 02:20:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:d66d7e4e-b038-47c2-bc26-8d5db9f47069</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-86386</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by mac cosmetics</title>
      <description>&lt;p&gt;thanks for taking the time in making it and sharing it
now everytime someone asks a question related to B&amp;#38;W coloring I will refer them to this list&lt;/p&gt;</description>
      <pubDate>Mon, 18 Apr 2011 02:18:51 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:dec5ab69-5777-4c0f-a68e-57b4a7ba2812</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-86383</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by ipad bag</title>
      <description>&lt;p&gt;TopCombine Follow &lt;a href="http://www.voien.com/" rel="nofollow"&gt;ipad bag&lt;/a&gt;  the detail tips below, you can increase the laptop battery life of a year or more. &lt;a href="http://www.fjblcy.com/" rel="nofollow"&gt;Game Controllers&lt;/a&gt; first thing you should care about the  &lt;a href="http://www.dewkett.com/" rel="nofollow"&gt;USB Gadgets&lt;/a&gt;  END!444444444444444444444&lt;/p&gt;</description>
      <pubDate>Fri, 18 Mar 2011 03:28:34 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:22c46bac-5599-4bc3-836c-5f207a4dbedc</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-73477</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by Tenant Screening</title>
      <description>&lt;p&gt;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.&lt;/p&gt;</description>
      <pubDate>Tue, 22 Feb 2011 11:13:29 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:2075bd4b-bd63-4b94-8497-c1e9baf9c0e1</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-65696</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by laptop accessories</title>
      <description>&lt;p&gt;JAY Follow &lt;a href="http://www.sagi2t.com/" rel="nofollow"&gt;laptop accessories&lt;/a&gt;  the detail tips below, you can increase the laptop battery life of a year or more. 1. The &lt;a href="http://www.diversitydefineit.com/" rel="nofollow"&gt;laptop battery&lt;/a&gt; first thing you should care about the&lt;/p&gt;</description>
      <pubDate>Mon, 21 Feb 2011 00:33:52 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:e922a1e2-dda1-463b-af6a-2dca12123060</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-64617</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by Criminal Records</title>
      <description>&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.&lt;/p&gt;</description>
      <pubDate>Fri, 18 Feb 2011 12:01:50 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:51234f2b-15ab-471a-86ac-08779576bcc0</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-64252</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by Pandora</title>
      <description>&lt;p&gt;has just told him that hand-washing is too expensive, and he should stop doing it.&#8221;&lt;/p&gt;</description>
      <pubDate>Thu, 02 Dec 2010 05:07:42 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:8b14438e-8576-4cfa-9ae9-8802ee67ba57</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-45411</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by 23@gmail.com</title>
      <description>&lt;p&gt;For those defective to &lt;a &gt;&lt;strong rel="nofollow"&gt;thomas sabo blog&lt;/strong&gt;&lt;/a&gt; after today&amp;#8217;s announcement, the &lt;a &gt;&lt;strong rel="nofollow"&gt;links of london blog&lt;/strong&gt;&lt;/a&gt;  will still not a bargain. Now the band says that &amp;#8221; &lt;a &gt;&lt;strong rel="nofollow"&gt;buy Pandora blog&lt;/strong&gt;&lt;/a&gt; &amp;#8221; day duty are being booked for the fourth billet and 2005. The reports is so&lt;/p&gt;</description>
      <pubDate>Thu, 08 Jul 2010 21:21:53 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:8899e6dc-f4df-49b0-a0df-f2bf9b6f9490</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-15951</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by Mark Taylor - Crytek</title>
      <description>&lt;p&gt;The one problem with all such smart pointers is that they commit you to write code in which objects know how to freem themselves. An object that is not responsible for its own memory management is more easily put on the stack, or into arrays, or into some other aggregate.&lt;/p&gt;


	&lt;p&gt;I prefer a C style API.&lt;/p&gt;


	&lt;p&gt;A* CreateInstanceofA();
void Free(A* ptr);&lt;/p&gt;</description>
      <pubDate>Mon, 30 Nov 2009 03:56:13 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:0b120e70-5da7-435e-b745-1cf185200b3c</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-5341</link>
    </item>
    <item>
      <title>"Single Ownership in C++ (intro to auto_ptr)" by suryamargha19@yahoo.com</title>
      <description>&lt;p&gt;Hi,
That was cooooool&amp;#8230;The article ws really written well. I was having trouble to undertstand the owning and aliasing semantics and it cleared my doubt..
Thanks a lot&amp;#8230;&lt;/p&gt;


	&lt;p&gt;Regs,
Surya&lt;/p&gt;</description>
      <pubDate>Sun, 05 Apr 2009 09:12:17 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:90a148c9-00e4-4a8f-a636-c04ee86a23f3</guid>
      <link>http://blog.objectmentor.com/articles/2007/03/30/single-ownership-in-c-intro-to-auto_ptr#comment-3105</link>
    </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 -0500</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>

