I have a code that does something like following example, and I'm not sure if this is correct because the executable runs as expected.
// source.cpp
void compute_x(int& ref)
{
ref = 0;
}
void f(int x)
{
int local = x;
local = 1;
if (local)
{
return copute_x(local);
}
else return;
}
int main()
{
f(2);
return 0;
}
the code runs but, Is variable local
valid once f
returns?
The variable local
goes out of scope after f
returns.
After your edit : but the return value is returned from the function and subsequently returned from main
.
否,变量local是无效的,因为其作用域位于f函数定义的括号之间...此后不存在
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.