Returning the address of local or temporary variable
- by Dave18
#include <iostream>
int& foo()
{
int i = 6;
std::cout << &i << std::endl;
return i;
}
int main()
{
int i = foo();
std::cout << &i << std::endl;
}
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?