简体   繁体   English

使用std :: string而不是char * bad?

[英]Is using std::string instead of char* bad?

I'm beginning with C++ and I am little confused whether to use std::string or char* . 我从C ++开始,我很困惑是否使用std::stringchar*

Lets say I need to concatenate a few strings. 让我们说我需要连接几个字符串。 I'd do it like this - I think it's the shortest way. 我这样做 - 我认为这是最短的方式。

std::string data = "some data";
const char *result = std::string("start" + data + "end" + "something else").c_str();

Question is: Should I do it this way or use c-functions like strcpy() , strcat() etc.? 问题是:我应该这样做还是使用像strcpy()strcat()等c函数? Is using std::string slower, less effective than char* ? 使用std::stringchar*更有效吗?

Thanks 谢谢

const char *result = std::string("start" + data + "end" + "something else").c_str(); is a recipe for disaster . 是一个灾难的食谱

The right hand of the assignment is using an anonymous temporary and will leave a dangling pointer result . 赋值的右手使用匿名临时,并留下悬空指针result

The idea of converting std::string data = "some data"; 转换std::string data = "some data";的想法std::string data = "some data"; to char* data = "some data"; to char* data = "some data"; is not emphasising the fact that "some data", after the conversion from std::string becomes read-only memory . 并没有强调从std::string转换后的“某些数据”成为只读内存的事实。 Modification of the memory to which data is pointing to is undefined behaviour. 修改data所指向的内存是未定义的行为。 At least use a const char* so compilation will fail on such an attempt. 至少使用const char*因此编译将在这样的尝试中失败。

All these problems go away if you use std::string . 如果你使用std::string所有这些问题都会消失。 There's your answer. 有你的答案。 It considerably simplifies things. 它大大简化了事情。

As many have already said in the comments, always use std::string . 正如许多人在评论中已经说过的那样,总是使用std::string It is better than null-terminated strings (C / char* strings) in almost every way. 它几乎在所有方面都优于以null-terminated字符串(C / char *字符串)。

However the way you used string in your example is not correct and involves undefined behaviour. 但是,在示例中使用字符串的方式不正确并且涉及未定义的行为。

const char *result = std::string("start" + data + "end" + "something else").c_str();

In this line you created a temporary string . 在这一行中,您创建了一个临时string As I hope you know, temporary variables only last until the end of the row (as far as this example goes at least). 我希望你知道,临时变量只持续到行结束(至少这个例子)。 So after this row the string will not exist anymore. 所以在这一行之后, string将不再存在。 A string has an internal array on heap in which it stores its string . string在堆上有一个内部数组,用于存储其string The function string::c_str() returns a pointer to the first character of that array. 函数string::c_str()返回指向该数组的第一个字符的指针。 But because the string object gets destroyed at the end of the line, the pointer result will point to a deleted part of the memory and accessing it is undefined behaviour. 但是因为字符串对象在行的末尾被破坏,指针结果将指向内存的已删除部分并且访问它是未定义的行为。

std::string data = "some data";
std::string bigData("start" + data + "end" + "something else");
const char *result = bigData.c_str();

If you wrote your code like this, then there is no undefined behaviour since result 's lifetime is shorter than the string bigData . 如果您编写了这样的代码,则没有未定义的行为,因为result的生命周期比字符串bigData短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: string getting(char *)而不是(const char *) - std::string getting (char *) instead of (const char *) main()std :: string代替char **为什么排除 - main () std::string instead of char** why the exclusion 在c ++中只使用std :: string而不是char数组和std :: vector / list而不是数组有任何实际限制吗? - Are there any practical limitations to only using std::string instead of char arrays and std::vector/list instead of arrays in c++? 需要使用 std::string 用 const char* 填充数组,但它不起作用(我认为它正在存储指针)? - Need to fill an array with const char* using std::string but it's not working(i think it's storing the pointers instead)? 在结构向量中,我应该在结构中使用 char[] 而不是 std::string 以便它们是 POD 吗? - In a vector of structs, should I be using char[] instead of std::string in structures so that they are POD? 使用.getline和字符串代替char - Using .getline with string instead of char ifstream的std :: getline,使用参数字符串或char * - std::getline for an ifstream, using parameter string or char * 使用const cast将标准字符串转换为char * - Converting a std string to char* using const cast 使用std :: string构造函数复制const char * - Copy of const char * using std::string constructor 传递const char而不是std :: string作为函数参数 - passing a const char instead of a std::string as function argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM