S
Sachin Midha
string func()
{
string msg="ABC";
return msg;
}
int main()
{
string s;
s=func();
cout<<s;
return 0;
}
The program prints s as "ABC", but msg was a local string of func()
and not of main & hence the output should have been any garbage value
but not the same string as "ABC".
Is it occuring because on the top of the stack(that gets generated on
function call), the value of msg remains and it is assigned to
variable s??
But then, if I want to verify that, how can I call another function in
between the call to func() and assignment of the returned value to s??
or if there is some other reason for the program printing ABC, please
let me know, this kind of problem has been following me for a long
time now.
Thanks.
{
string msg="ABC";
return msg;
}
int main()
{
string s;
s=func();
cout<<s;
return 0;
}
The program prints s as "ABC", but msg was a local string of func()
and not of main & hence the output should have been any garbage value
but not the same string as "ABC".
Is it occuring because on the top of the stack(that gets generated on
function call), the value of msg remains and it is assigned to
variable s??
But then, if I want to verify that, how can I call another function in
between the call to func() and assignment of the returned value to s??
or if there is some other reason for the program printing ABC, please
let me know, this kind of problem has been following me for a long
time now.
Thanks.