Does returning newly-declared strings cause leaks?

This is a discussion on Does returning newly-declared strings cause leaks? within the C++ Programming forums, part of the General Programming Boards category; Serious question. Code: string createString() { string some_string = "string"; return some_string; } Does this leak? I obviously can't delete ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    1

    Does returning newly-declared strings cause leaks?

    Serious question.

    Code:
    string createString()
    {
    string some_string = "string";
    return some_string;
    }
    Does this leak? I obviously can't delete some_string from within this function.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    No, not in this context. You're actually returning a copy of the string object.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    There is no call to new, so there doesn't need to be any call to delete.

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Returning a string that's allocated on the stack in a function is undefined behavior. You might be able to use it, but it's not safe.

    Declare a pointer in the function and allocate memory with new, and return the point. That way, it's allocated on the heap.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> Returning a string that's allocated on the stack in a function is undefined behavior. You might be able to use it, but it's not safe.

    This is wrong (at least in the context of the thread). The OP's code is safe and good. It is also better than returning something allocated on the heap.

    A C++ string is an object that will be properly copied and destroyed as appropriate when you return a string object from the function.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Daved View Post
    >> Returning a string that's allocated on the stack in a function is undefined behavior. You might be able to use it, but it's not safe.

    This is wrong (at least in the context of the thread). The OP's code is safe and good. It is also better than returning something allocated on the heap.

    A C++ string is an object that will be properly copied and destroyed as appropriate when you return a string object from the function.
    Ah, sorry. My bad. More of a C programmer than a C++ programmer, I didn't think the object would be copied.

    But you can't deny that it's slower to return a copy of the object than to return a pointer to it, but the difference is negligible unless the function is called often.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    IceDane's concern would apply if one returned a reference instead of a copy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> But you can't deny that it's slower to return a copy of the object than to return a pointer to it,
    >> but the difference is negligible unless the function is called often.

    It's probably slower to return a copy of the string than a pointer, true, but there are a couple other things to remember about that. RVO will often skip the copy there, easing any performance hit. Also, if you return a pointer to dynamically allocated memory, the slowdown from that versus local stack variables might be a greater issue.

    In the end, it will rarely be a problem, so the clarity of returning the copy usually makes sense, IMO.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    And if you really that concerned about performance (which would only be applicable if this function is called several million times in a row), you could use a reference:
    Code:
    void GetString( std::string&  output )
    {
        output = "Hello World";
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  2. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM
  3. Passing & Returning Strings from Functions
    By nisaacs in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 04:34 AM
  4. Class accessor functions returning strings?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-31-2001, 11:48 AM
  5. returning strings
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 09-06-2001, 10:38 PM

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