开发者

String (C++) in XCode

Does anyone know why in XCode, when you do something simple like

string str;
cout << "input string";
getline(cin, str);
cout << str;

you would get malloc: *** error f开发者_StackOverflowor object 01x100000240: pointer being freed was not allocated error? thanks.


It's a bug in xcode. To fix it, paste these lines at the very beginning of your program (before any #include statements):

#define _GLIBCXX_FULLY_DYNAMIC_STRING 1
#undef _GLIBCXX_DEBUG
#undef _GLIBCXX_DEBUG_PEDANTIC


This sounds like a bug in your implementation. You may have left out something important, try a complete test case:

#include <iostream>
#include <ostream>
// iostream not required to declare operator<<(ostream&,char const*)
// but ostream is
#include <string>

int main() {
  using namespace std;
  cout << "Input: ";
  string line;
  if (!getline(cin, line)) {
    clog << "Input error.\n";
    return 1;
  }
  cout << "You entered: " << line << '\n';
  return 0;
}


I found a couple references to this bug in XCode via Google. The best workaround I found was

The solution is to double-click on the target to open its Info window, go to the Build tab, and scroll down to the "GCC 4.2 - Preprocessing" section. In this section is a setting named "Preprocessor Macros" that by default has two entries, "_GLIBCXX_DEBUG=1" and "_GLIBCXX_DEBUG_PEDANTIC=1". Remove these entries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜