C++ 中的 istringstream
我确信我只是在这里做了一些愚蠢的事情,但我不太明白它是什么。当我尝试运行此代码时:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以答案是 Xcode 中的一个错误。 这是类似的问题及其解决方案。
So the answer turned out to be a bug in Xcode. Here's a similar problem and its solution.
对我有用。
但我从来没有这样做过:
尝试一下
Works for me.
But I have never done this:
Try