Returning the address of local or temporary variable
Posted
by Dave18
on Stack Overflow
See other posts from Stack Overflow
or by Dave18
Published on 2010-04-30T11:52:21Z
Indexed on
2010/04/30
11:57 UTC
Read the original article
Hit count: 187
c++
#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?
© Stack Overflow or respective owner