Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that a raw new does not specify ownership. If I new up an object and return it, who owns it? Does the creating function/object own it, or does the calling function? If you return smart pointers (std::shared_ptr and std::unique_ptr) you are specifying ownership.</p> <p>Not specifying ownership is one of the easiest ways to leak memory. I have the hardest time, even with professional programmers, getting people to understand ownership and work with it. This is mostly prevented by using good types (smart pointers) that specify ownership, just by existing.</p> <pre><code>type* function(); // Unspecified ownership. // Must be well documented and all users must read // and follow the documentation. std::unique_ptr&lt;type&gt; function(); // Calling function owns returned pointer. // Single ownership. std::shared_ptr&lt;type&gt; function(); // Calling function owns returned pointer. // Shared ownership. Can have multiple owners. std::weak_ptr&lt;type&gt; function(); // Calling function references returned pointer. // Must lock pointer to get owned object, if not deleted. // Shared ownership. Can have multiple owners. </code></pre> <p>These different types of pointers express ownership just by existing unlike raw pointers.</p> <p>As for <code>new</code> always being wrong. That is an overbroad generalization. <code>std::shared_ptr</code> is created using the global function <code>std::make_shared</code>. As of C++11 there is no <code>std::make_unique</code>, but that will be fixed in C++14. The only way to create a <code>std::unique_ptr</code> is to use new and <em>immediately</em> assign the pointer to a <code>std::unique_ptr</code>.</p> <p>There are also places where you would want a raw pointer and to manually use <code>new</code> and <code>delete</code>, but they tend to be very low level and most programmers will rarely encounter them.</p> <hr> <p>What really has me cringing about your code is not that you are using <code>new</code> but that you are dereferencing the pointer and assigning it to a reference. It would be almost impossible to guarantee that the destructor will ever be called. It also tends to leak memory, though in the case of assigning to a static variable it will be deallocated at program termination so you aren't really looking at memory leaking.</p> <pre><code>MyClass&amp; MyClass::MyInstance() { static MyClass &amp; myLocalVariable = * new MyClass(/*parameters*/); return myLocalVariable ; } </code></pre> <p>I would prefer to create to have the static variable be by value than by reference. This prevents putting the object on the heap. Depending on <code>MyClass</code> is could also allow the object to be mapped to memory from the executable without having to run any code to initialize it.</p> <pre><code>MyClass&amp; MyClass::MyInstance() { static MyClass myLocalVariable(/*parameters*/); return myLocalVariable ; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload