代码之家  ›  专栏  ›  技术社区  ›  LandonSchropp

C++中的数据流

  •  1
  • LandonSchropp  · 技术社区  · 15 年前

    我肯定我只是在做些傻事,但我不太明白这是什么。当我尝试运行此代码时:

    #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,但我认为字符串在堆上管理自己的内容。如有任何帮助,我们将不胜感激。

    谢谢,

    螺旋形的

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

    2 回复  |  直到 12 年前
        1
  •  1
  •   Loki Astari    15 年前

    为我工作。

    但我从未这样做:

    istringstream input(s, istringstream::in); 
    

    尝试

    istringstream input(s); 
    
        2
  •  2
  •   Community Mohan Dere    8 年前

    所以答案是xcode中的一个bug。 Here's 一个类似的问题及其解决办法。