title: Why does reference pointing to freed memory work?
body: <p>I tried below C++ code</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      int *p = new int;
      *p = 10;
      int &amp;a = *p;
      delete p;
      a = 20;
      cout&lt;&lt;a&lt;&lt;" ";
      cout&lt;&lt;*p;
      return 0;
  }
  </code></pre>
  
  <p>and got output as: 20 20</p>
  
  <p>I thought this may cause runtime error as result of accessing freed memory or some garbage. Probably I got this output as memory location freed by program may haven't been used so far so still retaining old values.</p>
  
  <p>So I thought that it should also happen if I don't use references</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
      int *p = new int;
      *p = 10;
  //  int &amp;a = *p;
      delete p;
  //  a = 20;
  //  cout&lt;&lt;a;
      cout&lt;&lt;*p;
      return 0;
  }
  </code></pre>
  
  <p>but in this case I got output as 0 (checked with multiple runs). Does Reference has anything to do with different outputs?</p>
  
  <p>Compiler: gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)</p>