Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to watch out for when converting a std::string to a char* for C function?

I have read many posts asking the question on how to convert a C++ std::string or const std::string& to a char* to pass it to a C function and it seems there is quite a few caveat's in regards to doing this. One has to beware about the string being contiguous and a lot of other things. The point is that I've never really understood all the points one needs to be aware of and why?

I wondered if someone could sum up the caveats and downfalls about doing a conversion from a std::string to a char* that is needed to pass to a C function?

This when the std::string is a const reference and when it's just a non-const reference, and when the C function will alter the char* and when it will not alter it.

like image 331
Tony The Lion Avatar asked Apr 12 '11 08:04

Tony The Lion


People also ask

Can you convert string to char in C++?

The c_str() and strcpy() function in C++C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0').

Is std::string a char *?

h functions when you are declaring string with std::string keyword because std::string strings are of basic_string class type and cstring strings are of const char* type.

What is a difference between the std::string and C style strings?

std::string is compatible with STL algorithms and other containers. C strings are not char * or const char * ; they are just null-terminated character arrays. Even string literals are just character arrays.

What is a char * in C++?

char* is typically used to iterate through a character array, i.e., a C string. It is rarely used as a pointer to a single char, unlike how other pointers are typically used. C++ has newer constructs for strings that typically should be used.


1 Answers

First, whether const reference or value doesn't change anything.

You then have to consider what the function is expecting. There are different things which a function can do with a char* or a char const*---the original versions of memcpy, for example, used these types, and it's possible that there is still such code around. It is, hopefully, rare, and in the following, I will assume that the char* in the C function refer to '\0' terminated strings.

If the C function takes a char const*, you can pass it the results of std::string::c_str(); if it takes a char*, it depends. If it takes a char* simply because it dates from the pre-const days of C, and in fact, it modifies nothing, std::string::c_str() followed by a const_cast is appropriate. If the C function is using the char* as an out parameter, however, things become more difficult. I personally prefer declaring a char[] buffer, passing this, and then converting the results to std::string, but all known implementations of std::string use a contiguous buffer, and the next version of the standard will require it, so correctly dimensioning the std::string first (using std::string::resize(), then passing &s[0], and afterwards redimensionning the string to the resulting length (determined using strlen(s.c_str()), if necessary) can also be used.

Finally (but this is also an issue for C programs using char[]), you have to consider any lifetime issues. Most functions taking char* or char const* simply use the pointer, and forget it, but if the function saves the pointer somewhere, for later use, the string object must live at least as long, and its size should not be modified during that period. (Again, in such cases, I prefer using a char[].)

like image 149
James Kanze Avatar answered Nov 15 '22 22:11

James Kanze