简体   繁体   中英

Is it a good idea to return " const char * " from a function?

Now I have a function that has to return a string. I saw a particular implementation where he returns a const char * from the function.

Something like this:

const char * GetSomeString() 
{ 
  ........   
  return somestlstring.c_str(); 
}

SomeOtherFoo ()
{
  const char * tmp = GetSomeString();
  string s = tmp;
}

Now I felt there is something potentially wrong with this. Is my gut feel right? or Is this a perfectly safe code?

Kindly give me ur suggestions. I have a feeling return const char * this way might result in havoc..

Thanks, Arjun

Depending on what somestlstring is and what is being done there.

If it is a local variable you are returning a pointer into memory that is being released when GetSomeString completes, so it is a dangling pointer and an error.

It all boils down to the lifetime of somestlstring and the operations you perform on it. The pointer returned by .c_str() is guaranteed to be valid only up to the next mutating operation in the string. So if something changes somestlstring from the call to .c_str() and before s is constructed you will be in undefined behavior land.

If you are asking about the lifetime of the const char * returned by the std::string c_str() function, it is valid until you modify the string you obtained it from, or until the string is destroyed. Returning it from a function is OK (though I would say not great practice), provided you bear those two facts in mind.

This is OK under the conditions @Neil elaborated on. However a better way would be to return a reference to the string

string const& GetSomeString() 
{ 
  ........   
  return somestlstring; 
}

string s = GetSomeString();

Still keeping in mind that ´somestlstring` must not be a local automatic variable but stored somewhere else in a namespace or a class. Otherwise you can return the string by value

string GetSomeString() 
{ 
  ........   
  return somestlstring; // can be a local automatic variable
}

string s = GetSomeString();

It's not great - how long does the memory for your string stick around? Who is responsible for deleting it? Does it need to be deleted at all?

You're better off returning a string object that is responsible for allocating and freeing the string memory - this could be a std::string, or a QString (if you're using Qt), or CString (if you're using MFC / ATL).

on a slightly different note, will your string ever be unicode? Most string classes can deal transparently with unicode data, but const char will not...

To add some scenarios in which this would be ok:

  • somestlstring is a global variable initialized in the same translation unit (.cpp) as GetSomeString()
  • somestlstring is a non-static class member, and GetSomeString is a member of that class. In that case, the lifetime of the pointer returned must be documented (basically - as others said - until the strign changes or the object is destroyed)
  • you are returning a char const * to a literal or compile-time initialized string

It depends upon where the somestlstring variable is located.

If it is a variable locale to the GetSomeString() function, then this is plainly wrong. Indeed, the somestlstring variable is destroyed at the end of the function, and thus the const char * points to something that does not exist anymore.

If it is a global variable, then this code is right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM