如何使用“”初始化 std::string?
我在使用 ""
(即空字符串)初始化 std::string 变量时遇到问题。它会导致以前有效的代码出现奇怪的行为。下列说法错误的是?
std::string operationalReason = "";
当我使用以下代码时,一切正常:
std::string operationalReason;
operationalReason.clear();
我相信字符串文字存储在依赖于编译器的单独内存位置中。我看到的问题实际上是否表明该存储已损坏?如果是这样,它会被我使用 clear()
函数隐藏。
谢谢。
I'm facing problems with initializing a std::string variable using ""
(i.e. an empty string). It's causing strange behavior in code that was previously working. Is the following statement wrong?
std::string operationalReason = "";
When I use the following code everything works fine:
std::string operationalReason;
operationalReason.clear();
I believe that string literals are stored in a separate memory location that is compiler-dependent. Could the problem I'm seeing actually be indicating a corruption of that storage? If so, it would get hidden by my usage of the clear()
function.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它调用默认构造函数,并创建一个空字符串。
所以我想说
std::stringoperativeReason = ""
是多余的。It invokes the default constructor, and creates an empty string anyway.
So I would say
std::string operationalReason = ""
is overkill.std::stringoperativeReason = "";
从技术上讲,这完全没问题,但更常见和更好的是
std::stringoperativeReason;
字符串的默认构造函数将创建一个空字符串
是的,关于字符串文字存储在不可变内存中,等等,你是对的……但是字符串复制器总是复制传递的字符串或 C 字符串
std::string operationalReason = "";
This is perfectly fine, technically, but more common and nice is just
std::string operationalReason;
The default ctor of the string will create an empty string
Yes, you are right about string literals being stored in a nonmutable memory blah blah etc etc... but the string copy-ctor always copies the string or C-string passed
如果您只执行
std::stringoperativeReason;
会发生什么?这应该与您提供的两个示例具有相同的效果。事实上,如果您在使用std::stringoperativeReason = "";
表单时遇到问题,这可能表明字符串数据存储已损坏,但也可能意味着其他部分内存已损坏,并且该特定行导致其表现不同。当您使用
""
表单时或稍后在运行时,您的代码是否会立即崩溃?您是否可以在 valgrind 或类似工具下运行它来查看是否发现内存问题?如果将字符串初始化为""
之外的某个文字,会发生什么情况?What happens if you just do
std::string operationalReason;
? That should have the same effect as the two examples you provided. If in fact you're experiencing problems when you use thestd::string operationalReason = "";
form that may indicate that the string data storage has been corrupted, but it may equally mean that some OTHER part of memory is corrupted and that particular line causes it to manifest differently.Does your code crash immediately when you use the
""
form or later on at runtime? Are you able to run this under valgrind or similar to see if it spots memory problems? What happens if you initialized the string to some literal other than""
?两种形式都已更正,但这种形式:
它调用以
const char *
作为参数的构造函数。首先它调用默认构造函数,然后尝试复制数据,在本例中什么也不复制。是优选的。
您可以使用
clear()
将其重置为空字符串。The two forms are corrected, but this one:
It calls the constructor which takes a
const char *
as argument. First it calls the default constructor and then it tries to copy the data, in this case nothing.is preferable.
You can use
clear()
to reset it to an empty string.