Related to pointers memory allocation
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/4570366/pointer-to-local-variable">Pointer to local variable</a><br>
  <a href="http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope">Can a local variable&#39;s memory be accessed outside its scope?</a>  </p>
</blockquote>



<pre><code>int* fun()
{
    int i = 10;
    int*ptr = &amp;i;
    return ptr;
}// variable i and ptr will be destroyed automatically as ctrl goes out of scope
void main()
{
    cout&lt;&lt;"i: "&lt;&lt;*(fun())&lt;&lt;endl;
    getch();
}
</code></pre>

<blockquote>
  <p>Output: i: 10</p>
</blockquote>

<p>My doubt is that why the output is coming as <code>i=10</code>.</p>

<p>As per my understanding, as soon as the fun() goes out of scope, variable i will be destroyed automatically, so o/p should be some garbage value...</p>

<p>I executed this on Visual Studio 2008</p>
