Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

string

People also ask

What is const char * used for?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

Can a char * be passed as const * argument?

In general, you can pass a char * into something that expects a const char * without an explicit cast because that's a safe thing to do (give something modifiable to something that doesn't intend to modify it), but you can't pass a const char * into something expecting a char * (without an explicit cast) because that's ...

What is the difference between const char * and char * const?

The difference is that const char * is a pointer to a const char , while char * const is a constant pointer to a char . The first, the value being pointed to can't be changed but the pointer can be. The second, the value being pointed at can change but the pointer can't (similar to a reference).

Can you assign a const char * to a string?

You absolutely can assign const char* to std::string , it will get copied though. The other way around requires a call to std::string::c_str() .


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.