title: About Heap and Scope
body: <p>I have this very simple code:</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      int *p, *q, u;
      
      {
          p = new int(4);
          int v = 5;
          q = &amp;v;
          u= v;
      }//End scope 2
      
      cout&lt;&lt; *p &lt;&lt; &quot;\n&quot; &lt;&lt; *q &lt;&lt; &quot;\n&quot; &lt;&lt; u;
      delete p;
  
      return 0;
  }
  </code></pre>
  <p>And the output is:</p>
  <pre><code>4
  5
  5
  </code></pre>
  <p>Pleaso cerrect me if I'm wrong :) ...</p>
  <ul>
  <li>if I understand, <code>p</code> is pointing to a memory space that was allocated in the heap, this memory won't be affecetd at the &quot;end scope&quot; and will only be dellocated when calling <code>delete</code>.</li>
  <li>Then <code>u</code> is a hard copy of <code>v</code>, so there's actually a copy of it's value  somewhere else in the stack memory.</li>
  <li>On the other hand <code>q</code> points to the address of <code>v</code>, which is allocated in the stack and should be erased (?) when leaving the &quot;scope 2&quot;, if this is the case, why printing <code>*q</code> still finds the right value of <code>v</code>?</li>
  </ul>