About the bug correction of some QString to char* and const char* on the Internet

Recently, I happened to be looking up information in this regard, and saw

Some articles on the Internet explain the method of converting QString to char* and const char* as follows:

QString str(“hello world!”);

Convert to const char *:

const char * arr = str.toStdString.c_str();

Convert to char * :

char * arr = str.toStdString.data();

However, let's not talk about toStdString without brackets first, the correct one is toStdString(). This code itself has some problems:

const char* and char* do not directly store the characters in the string, they are pointers, pointing to a piece of memory space, and the characters in the string can only be found in the memory space. After QString executes the toStdString() function, it returns std::string, and whether it is c_str() or data(), it returns a pointer to the storage space for storing characters in this std::string, so first of all The existence of this string, then the results returned by these functions will be accurate.

But as we all know, after the function ends, the return value is destroyed after being received by a variable (or the like). Therefore, even if it has not been destroyed when c_str() and data() are executed, when the pointer of the storage space of std::string is assigned to arr, std::string has been destroyed, so the memory pointed to by the arr pointer is finally The value of the space can only be empty or garbled. (The description may be somewhat inaccurate, please correct me) Visual Studio's explanation for this error is:

So this method cannot store the transferred characters in arr for a long time.

And the following code is correct:

std::string stdstr = str.toStdString(); 

Because the equal sign for std::string is a copy of the instance, unlike a pointer, although the equal sign in the pointer is a copy address, but the value in the address is not copied to a new one, and the new pointer still points to original memory space.

Solutions vary by situation.

According to the above description, it can be obtained: the easiest way is to persist std::string, such as the following:

std::string stdstr = str.toStdString();
char* arr = stdstr.data();
const char* arr = stdstr.c_str();

But if stdstr is only a variable in a function, and arr needs to be passed outside the function, because the variables inside will also be destroyed after the function is executed, then other methods need to be used, such as using std::string in a larger scope Persistence, etc., please study the specific situation by yourself.

If there are inappropriate descriptions or errors in the article, please discuss and correct them in the comment area, and some problems can also be discussed and exchanged in the comment area.

If you think this article is meaningful, please give it a lot of likes, favorites and support, and you can also click the "Reward" button below to reward and support me~

Guess you like

Origin blog.csdn.net/cgy13347250452/article/details/128951199