[英]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::string
或char*
。
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::string
比char*
更有效吗?
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.