Sign in to follow this  

std::string or char* as parameters to function in C++?

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there a preferred way to use strings as parameters? I know that you usually should use std::string wherever possible instead of char*. If we have the two function proptypes below: void myfunction(char* mystring); void myfunction(std::string mystring); I think that the first version may be as good as the second and perhaps even better since it gives the user of the function the option to either use char* or std::string (with c_str()) when calling the function. If the user is programming with C he cannot use std::string. Internally both functions can use std::string to do string operations. Does it cost a lot in performance to call std::string::c_str() and to create a std::string from a char*? /roq

Share this post


Link to post
Share on other sites
It is not possible to know which way is better performance-wise without knowing how the parameter is used. std::string might be slower because it means an extra indirection, but if you are going to do that indirection anyway, then there is no advantage to char *. If you need the length of the string then std::string will be faster because the length does not have to be computed.

Anyway, the safety, flexibility, and functionality of std::string is generally more important than the few nanoseconds you might save using char *.

BTW, the function parameters would look like this:
    void myfunction(char* mystring);
void myfunction(std::string & mystring);
Quote:
Does it cost a lot in performance to call std::string::c_str() and to create a std::string from a char*?

std::string::c_str() is probably very fast (just an educated guess) and std::string( char * ) is probably slow (because it may involve memory allocation and copying).

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton

void myfunction(char* mystring);
void myfunction(std::string & mystring);


Or if the string parameters are treated as read only values:

void myfunction(const char * mystring);
void myfunction(const std::string & mystring);

Share this post


Link to post
Share on other sites
If at all possible try to use reference argument to fill in a string or return a reference to a string if posible, but try to avoid return a string.

void getSomeThing(string& fill);
string& getSomeThingByRef(); // can change return string
const string& getSomeThingByRefConst(); // can't change return string
instead of:
string getSomeThing(); // could invole 1 or more copies for strings

Lord Bart :)

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by JohnBolton

void myfunction(char* mystring);
void myfunction(std::string & mystring);


Or if the string parameters are treated as read only values:

void myfunction(const char * mystring);
void myfunction(const std::string & mystring);


resurrecting this thread a little, but would it be a faux pas to just have

void myfunction(const std::string & mystring); 


rather then

void myfunction(const char * mystring);
void myfunction(const std::string & mystring);


- and just let the implicit conversion of a char* to std::string take place?

Share this post


Link to post
Share on other sites
Doing that usually isn't a problem. You might get better performance out of having both in some situations, but that's something you can worry about if your profiler tells you there is an issue with the function.

Share this post


Link to post
Share on other sites
Quote:
Original post by paulecoyote
Quote:
Original post by SiCrane
Quote:
Original post by JohnBolton

void myfunction(char* mystring);
void myfunction(std::string & mystring);


Or if the string parameters are treated as read only values:

void myfunction(const char * mystring);
void myfunction(const std::string & mystring);


resurrecting this thread a little, but would it be a faux pas to just have

void myfunction(const std::string & mystring); 


rather then

void myfunction(const char * mystring);
void myfunction(const std::string & mystring);


- and just let the implicit conversion of a char* to std::string take place?


But the original problem was allowing C programmers to use the function (i.e. when std::string wasn't available).

Back to the original poster, either write your library in straight C or go for C++ - DON'T try and make comprimises for both as it will lead to an interface that is cluttered and hard to maintain.


// Good code - no jokes about g_string please ;)
void SetString (const std::string &src_string)
{
g_string = src_string;
}

const std::string &GetString ()
{
return g_string;
}



If the user needs to use the result of GetString in a legacy library, let him make the call to c_str himself.

Share this post


Link to post
Share on other sites

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this