如何使用“”初始化 std::string?

发布于 2024-10-23 00:29:32 字数 363 浏览 6 评论 0原文

我在使用 "" (即空字符串)初始化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哑剧 2024-10-30 00:29:33
std::string operationalReason; //is enough!

它调用默认构造函数,并创建一个空字符串。

所以我想说 std::stringoperativeReason = "" 是多余的。

std::string operationalReason; //is enough!

It invokes the default constructor, and creates an empty string anyway.

So I would say std::string operationalReason = "" is overkill.

红ご颜醉 2024-10-30 00:29:33

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

左岸枫 2024-10-30 00:29:33

如果您只执行 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 the std::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 ""?

蓝天白云 2024-10-30 00:29:33

两种形式都已更正,但这种形式:

std::string operationalReason = ""

它调用以 const char * 作为参数的构造函数。首先它调用默认构造函数,然后尝试复制数据,在本例中什么也不复制。

std::string operationalReason;

是优选的。
您可以使用 clear() 将其重置为空字符串。

The two forms are corrected, but this one:

std::string operationalReason = ""

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.

std::string operationalReason;

is preferable.
You can use clear() to reset it to an empty string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文