Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>DON'T! </p> <p>The only way to minimize pointer errors is to use <em>the right</em> pointer types. And that's <em>types</em>, plural.</p> <p>Shared pointers are not a silver bullet. They become memory leaks as soon when you have cyclical references (and if plan to use them <em>everywhere</em>, those will show up pretty quickly)</p> <p>If you want error-free C++ applications, you have to work for it. You have to understand your application. You have to understand the ownership semantics of different objects. Shared pointers just give you shared ownership, which is generally a decent lowest denominator. Everything else can be replaced by shared ownership and it'll work, sort of.</p> <p>But the default case is that an object is owned by <em>one</em> other entity. It is owned by a function, and should be destroyed when that function returns, or it is owned by a class, or whatever else. Often, you don't need pointers at all. the object is stored by value in a <code>std::vector</code>, perhaps. Or it is just a local variable or a class member. If it <em>is</em> a pointer, it'll often be better expressed by a <code>scoped_ptr</code> or perhaps one which allows transfer of ownership (<code>unique_ptr</code> or <code>auto_ptr</code>).</p> <p><code>shared_ptr</code> is what you might fall back to when you can give no guarantees about the lifetime or ownership of an object. But when you use that, you also need to use <code>weak_ptr</code> to break cycles.</p> <p>Really, a better approach is to <em>avoid</em> pointers as much as at all possible. When you <em>do</em> need a pointer, use one which has the most specific ownership semantics possible (prefer <code>scoped_ptr</code>, which doesn't allow transfer of ownership at all, then if you need it, fall back to one which allows you to <em>move</em> ownership, such as <code>unique_ptr</code>, and only as a last resort should you use <code>shared_ptr</code>, which allows you to share ownership freely among any number of clients.</p> <p>There's no magic wand you can wave to make your C++ code "just work". The only way to achieve that is to write good solid C++ code. And you do that by knowing, and using, the tools at your disposal, not by pretending that "hey, <code>shared_ptr</code> is just like a garbage collector, isn't it? I can just ignore all questions of object lifetime or memory leaks if I use it".</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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