简体   繁体   中英

Can I get a non-const C string back from a C++ string?

Const-correctness in C++ is still giving me headaches. In working with some old C code, I find myself needing to assign turn a C++ string object into a C string and assign it to a variable. However, the variable is a char * and c_str() returns a const char [] . Is there a good way to get around this without having to roll my own function to do it?

edit: I am also trying to avoid calling new. I will gladly trade slightly more complicated code for less memory leaks.

in C++1x, this should work:

foo(&s[0], s.size());

However this needs a note of caution: The result of &s[0] (as the result of s.c_str() , BTW) is only guaranteed to be valid until any member function is invoked that might change the string. So you should not store the result of these operations anywhere. The safest is to be done with them at the end of the full expression, as my examples do.


Pre C++-11 answer

Since for to me inexplicable reasons nobody answered this the way I do now, and since other questions are now being closed pointing to this one, I'll add this here, even though coming a year too late will mean that it hangs at the very bottom of the pile...


With C++03, std::string isn't guaranteed to store its characters in a contiguous piece of memory, and the result of c_str() doesn't need to point to the string's internal buffer, so the only way guaranteed to work is this:

std::vector<char> buffer(s.begin(), s.end());
foo(&buffer[0], buffer.size());
s.assign(buffer.begin(), buffer.end());

This is no longer true in C++11.

There is an important distinction you need to make here: is the char* to which you wish to assign this "morally constant"? That is, is casting away const -ness just a technicality, and you really will still treat the string as a const ? In that case, you can use a cast - either C-style or a C++-style const_cast . As long as you (and anyone else who ever maintains this code) have the discipline to treat that char* as a const char* , you'll be fine, but the compiler will no longer be watching your back, so if you ever treat it as a non- const you may be modifying a buffer that something else in your code relies upon.

If your char* is going to be treated as non- const , and you intend to modify what it points to, you must copy the returned string, not cast away its const -ness.

I guess there is always strcpy .

Or use char* strings in the parts of your C++ code that must interface with the old stuff.

Or refactor the existing code to compile with the C++ compiler and then to use std:string .

There's always const_cast...

std::string s("hello world");
char *p = const_cast<char *>(s.c_str());

Of course, that's basically subverting the type system, but sometimes it's necessary when integrating with older code.

If you can afford extra allocation, instead of a recommended strcpy I would consider using std::vector<char> like this:

// suppose you have your string:
std::string some_string("hello world");
// you can make a vector from it like this:
std::vector<char> some_buffer(some_string.begin(), some_string.end());
// suppose your C function is declared like this:
// some_c_function(char *buffer);
// you can just pass this vector to it like this:
some_c_function(&some_buffer[0]);
// if that function wants a buffer size as well,
// just give it some_buffer.size()

To me this is a bit more of a C++ way than strcpy . Take a look at Meyers' Effective STL Item 16 for a much nicer explanation than I could ever provide.

You can use the copy method:

len = myStr.copy(cStr, myStr.length());
cStr[len] = '\0';

Where myStr is your C++ string and cStr a char * with at least myStr.length() +1 size. Also, len is of type size_t and is needed, because copy doesn't null-terminate cStr .

Just use const_cast<char*>(str.data())

Do not feel bad or weird about it, it's perfectly good style to do this.

It's guaranteed to work in C++11. The fact that it's const qualified at all is arguably a mistake by the original standard before it; in C++03 it was possible to implement string as a discontinuous list of memory, but no one ever did it. There is not a compiler on earth that implements string as anything other than a contiguous block of memory, so feel free to treat it as such with complete confidence.

If you know that the std::string is not going to change, a C-style cast will work.

std::string s("hello");
char *p = (char *)s.c_str();

Of course, p is pointing to some buffer managed by the std::string . If the std::string goes out of scope or the buffer is changed (ie, written to), p will probably be invalid.

The safest thing to do would be to copy the string if refactoring the code is out of the question.

If c_str() is returning to you a copy of the string object internal buffer, you can just use const_cast<> .

However, if c_str() is giving you direct access tot he string object internal buffer, make an explicit copy, instead of removing the const.

Since c_str() gives you direct const access to the data structure, you probably shouldn't cast it. The simplest way to do it without having to preallocate a buffer is to just use strdup.

char* tmpptr;
tmpptr = strdup(myStringVar.c_str();
oldfunction(tmpptr);
free tmpptr;

It's quick, easy, and correct.

std::string vString;
vString.resize(256); // allocate some space, up to you
char* vStringPtr(&vString.front());
// assign the value to the string (by using a function that copies the value).
// don't exceed vString.size() here!
// now make sure you erase the extra capacity after the first encountered \0.
vString.erase(std::find(vString.begin(), vString.end(), 0), vString.end());
// and here you have the C++ string with the proper value and bounds.

This is how you turn a C++ string to a C string. But make sure you know what you're doing, as it's really easy to step out of bounds using raw string functions. There are moments when this is necessary.

In CPP, if you want a char * from a string.c_str() (to give it for example to a function that only takes a char * ), you can cast it to char * directly to lose the const from .c_str()

Example:

launchGame((char *) string.c_str());

C++17 adds a char* string::data() noexcept overload. So if your string object isn't const, the pointer returned by data() isn't either and you can use that.

Is it really that difficult to do yourself?

#include <string>
#include <cstring>

char *convert(std::string str)
{
    size_t len = str.length();
    char *buf = new char[len + 1];
    memcpy(buf, str.data(), len);
    buf[len] = '\0';
    return buf;
}

char *convert(std::string str, char *buf, size_t len)
{
    memcpy(buf, str.data(), len - 1);
    buf[len - 1] = '\0';
    return buf;
}

// A crazy template solution to avoid passing in the array length
// but loses the ability to pass in a dynamically allocated buffer
template <size_t len>
char *convert(std::string str, char (&buf)[len])
{
    memcpy(buf, str.data(), len - 1);
    buf[len - 1] = '\0';
    return buf;
}

Usage:

std::string str = "Hello";
// Use buffer we've allocated
char buf[10];
convert(str, buf);
// Use buffer allocated for us
char *buf = convert(str);
delete [] buf;
// Use dynamic buffer of known length
buf = new char[10];
convert(str, buf, 10);
delete [] buf;

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