Returning the address of local or temporary variable
<pre><code>#include &lt;iostream&gt;

int&amp; foo()
{
    int i = 6;
    std::cout &lt;&lt; &amp;i &lt;&lt; std::endl; //Prints the address of i before return
    return i;
}

int main()
{
    int i = foo();
    std::cout &lt;&lt; i &lt;&lt; std::endl; //Prints the value
    std::cout &lt;&lt; &amp;i &lt;&lt; std::endl; //Prints the address of i after return
}
</code></pre>

<p>I know it doesn't return the address of local variable so that is why the warning but why does it still works and assign the variable i in main() to '6'? How does it only return the value if the variable the was removed from stack memory?</p>
