Why is it printing ABC

Discussion in 'C++' started by Sachin Midha, Sep 8, 2010.

  1. Sachin Midha

    Sachin Midha Guest

    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.
     
    Sachin Midha, Sep 8, 2010
    #1
    1. Advertising

  2. Sachin Midha

    Ian Collins Guest

    On 09/ 8/10 08:48 PM, Sachin Midha wrote:
    > 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.

    --
    Ian Collins
     
    Ian Collins, Sep 8, 2010
    #2
    1. Advertising

  3. Sachin Midha

    SG Guest

    On 8 Sep., 10:48, Sachin Midha wrote:

    > 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
     
    SG, Sep 8, 2010
    #3
  4. Sachin Midha

    Fred Zwarts Guest

    "Sachin Midha" <> wrote in message
    news:
    > 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.
     
    Fred Zwarts, Sep 8, 2010
    #4
  5. Sachin Midha

    cpp4ever Guest

    On 09/08/2010 09:48 AM, Sachin Midha wrote:
    > 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
     
    cpp4ever, Sep 8, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    835
    Jiong Feng
    Nov 19, 2003
  2. Bruce Sam
    Replies:
    15
    Views:
    7,929
    John C. Bollinger
    Nov 19, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,949
    Smokey Grindel
    Dec 2, 2006
  4. vsgdp

    ABC inheriting from ABC

    vsgdp, Sep 24, 2005, in forum: C++
    Replies:
    1
    Views:
    311
    vsgdp
    Sep 24, 2005
  5. Gunter Henriksen

    x.abc vs x['abc']

    Gunter Henriksen, May 13, 2009, in forum: Python
    Replies:
    1
    Views:
    354
    alex23
    May 15, 2009
Loading...

Share This Page