Returning reference to STL String class

This is a discussion on Returning reference to STL String class within the C++ Programming forums, part of the General Programming Boards category; Im trying to refresh my c++ skills.. its been years and Im a little confused if this is OK or ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    3

    Returning reference to STL String class

    Im trying to refresh my c++ skills.. its been years and Im a little confused if this is OK or not.
    I know you shouldn't return local variables, but I've seen this done before. Does the string class allocate its internal memory on the heap and so its ok.. thus returning just a pointer to the heap allocated object?
    Code:
    typedef std::string String;
    
    //Is this ok? why?
    String GetSomeString() {
      String ret;
    
      ret="bipbip";
      return ret;
    }
    
    
    //How does this differ.. can this lead to any memory corruption?
    //If I just use the returned value as a check.. 
    //if(GetSomeString=="bipbip")....
    String GetSomeString() {
      return "bipbip";
    }
    Thanks!
    Adam

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    both are fine and are equivalent. The trap you are trying not to fall into is returning a refernece to a local object. Code below demonstrates the flaw you should not fall into.
    Code:
    std::string&  flawfunc()
    {
       std::string retval;
       return retval;
    }
    Here i make a new std::string object then return a refernce to it as it gets deleted. So the calling function has a reference to a dead object. Dont do this.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    Thanks for the reply.
    So is a copy of the string object being performed when its returned in my case? I see the only differences are your function is defined as returning the & whereas Im returning a string object.
    Now if my object is being copied and then returned, can't you run into other problems with this (I seem to remember something about virtual functions getting goofy on copied objects).

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Yes you could but std::string is designed as a value type so there will be no trouble bar the efficiency loss of creating temporaries and copying.There should be no trouble with slicing anyway.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I know you shouldn't return local variables, but I've seen this done before.
    That is nothing wrong with it. If it is a user defined datatype a copy (the copy-constructor is invoked) is returned. The memory allocation/deallocation is maintained automatically by the string class, no worry there for memory leak.

    In the second example itīs more trickier because of the conversion mechanism. I think that that code snippet will compile (not recommended). The string class also supports operator overloading for operator==(const string&) and operator==(const char*) for backward compability. No memory leak is suffered.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    3
    it all makes sense now. Thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. objects
    By pktcperlc++java in forum C++ Programming
    Replies: 20
    Last Post: 03-04-2005, 05:05 PM
  4. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

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