tags:

views:

489

answers:

4

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 for object 01x100000240: pointer being freed was not allocated error? thanks.

+1  A: 

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;
}
Roger Pate
A: 

This looks like a bug in XCode, I'm also getting this problem.

kruczkowski
A: 

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.

SCFrench
+2  A: 

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
adam n