returning std::string

This is a discussion on returning std::string within the C++ Programming forums, part of the General Programming Boards category; My ? is, can you rely on the (value, pointer to char *, or whatever ) when you return a ...

  1. #1
    Unregistered
    Guest

    Question returning std::string

    My ? is, can you rely on the (value, pointer to char *, or whatever ) when you return a local std::string?

    For instance:

    std::string f(std::string first, std::string last)
    {
    std::string name = first + last;

    return name;
    }

    According to scoping rules, the memory occupied by the std::string variable "name" should be released back to the stack when the function exits. I see A LOT of code out there that does this very thing and the value returned is used later on. Are there special rules regarding std::string?

    thank you!

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    As you're returning a std::string object and not a pointer or reference to the one created in the function then a copy of the returned sting will be made when the string is returned to the calling function (assuming you are assigning the return value to a string).

    'name' will be out of scope, but 'name' won't be used after the function has returned.

    If you are returning a pointer or reference to the string then you cannot rely on the value if the string object was created in the function.
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    2

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    well heres how i hear it from Bjarne Stroustrupm no not personally but from his book, and i Assume your using MSVC since it has poblems with this very thing.

    >
    std::string f(std::string first, std::string last)
    {
    std::string name = first + last;

    return name;
    }
    <

    this is perfectly legal and should work,

    the compiler should setup a temporary variable to hold string and return it to the requesting variable. see example below

    Code:
    int func(void)
    {
        int a = 10;
        return a;
    }
    
    int main()
    {
        int k = func();
        printf("%d\n",k); // should print 10
        return 0;
    }
    
    the above should be equivalent in the result NOT IN ACTUALITY to the following
    
    int main()
    {
        int a = 10;
        k = a;
        printf("%d\n",k); 
        return 0;
    }
    Last edited by no-one; 09-24-2001 at 09:18 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Improving my code
    By rwmarsh in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2006, 11:18 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21