Why is it printing ABC

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.
 
I

Ian Collins

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".

What makes you think that? You are creating a string (assuming you are
using std::string) and returning it by value.
 
S

SG

string func() {
   string msg="ABC";
   return msg;
}

int main() {
string s;
s=func();

This is a default construction followed by an assignment. For
efficiency reasons, you should prefer a copy initialization:

string s = func();

Using this copy initialization your compier might be able to
completely elide all string object copies (g++ does that).
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".

No. Assuming by "string" you mean "std::string" this program is
perfectly fine. std::string is a type just like int or double with
respect to copying, assignment, etc. Since you return the string
object by value, everything is fine. It's like a string object manages
its own copy of the string value.

Even if you used "typedef char const* string;" instead of std::string
(please don't), the program would be fine because the string literal
"ABC" is a character array with static life-time.

Cheers!
SG
 
F

Fred Zwarts

Sachin Midha said:
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".

Why do you think that string is a special type?
If your reasoning would be correct, it would also hold for other types, like int.
It would be impossible for functions to return something else but a compile time constant.
But functions can return run-time computed values.
Whether this happens by using the stack or by using registers is an implementation detail,
which may vary for different compilers.
 
C

cpp4ever

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.

As your function is returning by value, (therein creating a copy), there
is no problem, also std::string msg will have made a copy of the "ABC"
string on initialisation. The sort of problem I suspect you are
considering is were a pointer/reference to a local variable from a
called function is used, see below.

std::string &func()
{
std::string msg = "MSG"
return msg; // Appalling
}

int main()
{
std::string &ret = func(); // Appalling
std::cout << ret;
return -1;
}

In the above case the ret variable is referencing a variable that is no
longer in scope, and cannot therefore be considered valid.

HTH

cpp4ever
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top