title: Program crashes on passing local variable address to shared pointer in c++
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  
  using namespace std;
  
  void func ()
  {
      cout &lt;&lt; "func\n";
      int localVariable = 10;
      int* p = new int;
      shared_ptr&lt;int&gt; ptr (&amp;localVariable); 
      shared_ptr&lt;int&gt; ptr1 (new int); 
      shared_ptr&lt;int&gt; ptr2 (p); 
  }
  
  int main ()
  {
      func();
      return 0;
  }
  </code></pre>
  
  <p>I tried to pass heap allocated memory directly to <code>shared_ptr</code> and also tried some pointer which is allocated before, both compile and run successfully.
  But when i tried to pass local variable's address to <code>shared_ptr</code>, it crashed with following stack :</p>
  
  <blockquote>
    <p>vidhu@~/Documents/CPP_Practice$ ./a.out  func
    <strong>* Error in `./a.out': free(): invalid pointer: 0xbfc7f13c *</strong></p>
    
    <p>Aborted (core dumped)</p>
  </blockquote>
  
  <p>Why this is happened ? I think deleting <code>NULL</code> pointer is safe but it is not good coding practice.</p>