C++ 中的 istringstream

发布于 2024-08-28 23:05:41 字数 736 浏览 0 评论 0原文

我确信我只是在这里做了一些愚蠢的事情,但我不太明白它是什么。当我尝试运行此代码时:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
 string s("hello");

 istringstream input(s, istringstream::in);

 string s2;
 input >> s2;

 cout << s;
}

我收到此错误:

malloc: *** error for object 0x100016200: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

我唯一能想到的是我在堆栈上分配了 s2,但我认为字符串在堆上管理自己的内容。任何帮助将不胜感激。

谢谢,

螺旋

编辑:修复了 main 的最后一行,其中 cout << s 应该是 cout << s2 。如果我将 s2 初始化为“hi”,它运行时不会出错,但否则不会出错。这只是一个奇怪的 gcc 编译问题吗?

I'm sure I'm just doing something stupid here, but I can't quite figure out what it is. When I try to run this code:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(int argc, char *argv[])
{
 string s("hello");

 istringstream input(s, istringstream::in);

 string s2;
 input >> s2;

 cout << s;
}

I get this error:

malloc: *** error for object 0x100016200: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

The only thing I can think of is that I allocated s2 on the stack, but I thought strings manage their own content on the heap. Any help here would be appreciated.

Thanks,

helixed

EDIT: Fixed the last line of main, where cout << s should have been cout << s2. It runs without error if I initialized s2 to "hi", but not otherwise. Is this just a weird gcc compilation problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

陈独秀 2024-09-04 23:05:41

所以答案是 Xcode 中的一个错误。 这是类似的问题及其解决方案。

So the answer turned out to be a bug in Xcode. Here's a similar problem and its solution.

过期情话 2024-09-04 23:05:41

对我有用。

但我从来没有这样做过:

istringstream input(s, istringstream::in); 

尝试一下

istringstream input(s); 

Works for me.

But I have never done this:

istringstream input(s, istringstream::in); 

Try

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