我确定我只是在这里做一些愚蠢的事情,但我无法弄清楚它是什么。当我尝试运行此代码时:
#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 编译问题吗?