title: What are all the ways use_count of std::shared_ptr is incremented?
body: <p>I have two <code>shared_ptr</code>s pointing to the same <code>int</code>, i.e. calling <code>get()</code> on them returns the same address. But calling <code>use_count()</code> on them returns <code>1</code>. When the last of them goes out of scope, it tries to free up the memory already freed by the other one, causing a double free runtime error:</p>
  
  <pre><code>#include &lt;memory&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      shared_ptr&lt;int&gt; sp1(make_shared&lt;int&gt;(7));
      shared_ptr&lt;int&gt; sp2(&amp;(*sp1));
      cout &lt;&lt; sp1.use_count() &lt;&lt; endl;  // 1
      cout &lt;&lt; sp2.use_count() &lt;&lt; endl;  // 1
      cout &lt;&lt; sp1.get() &lt;&lt; endl;        // same address
      cout &lt;&lt; sp2.get() &lt;&lt; endl;        // same address
  }
  // ^ Double free runtime error at closing brace.
  </code></pre>
  
  <p>Same thing happens in this variant with an explicitly declared raw pointer:</p>
  
  <pre><code>int main() {
      int *raw_ptr = new int(8);
      shared_ptr&lt;int&gt; sp3(raw_ptr);
      shared_ptr&lt;int&gt; sp4(raw_ptr);
      cout &lt;&lt; sp3.use_count() &lt;&lt; endl;  // 1
      cout &lt;&lt; sp4.use_count() &lt;&lt; endl;  // 1
      cout &lt;&lt; sp3.get() &lt;&lt; endl;        // same address
      cout &lt;&lt; sp4.get() &lt;&lt; endl;        // same address
  }
  // ^ Double free runtime error at closing brace.
  </code></pre>
  
  <p>Why does <code>use_count()</code> return <code>1</code> (but not <code>2</code>) if both <code>shared_ptr</code>s point to the same thing? If <code>use_count()</code> returned <code>1</code>, then why was there the attempt to free the <code>int</code> twice? I thought that a <code>shared_ptr</code> will increase <code>use_count</code> by one if and only if it points to the same address as its brethren <code>shared_ptr</code>s.</p>
  
  <p>Is a <code>std::shared_ptr</code>’s <code>use_count</code> incremented only by the first <code>shared_ptr</code>'s construction by raw pointer (or assignment to raw pointer, if default-constructed) and then by additional <code>shared_ptr</code>s’ copy-construction or assignment by any of the prior <code>shared_ptr</code>s? And what are all the other ways of incrementing, if any?</p>