5

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
  • This looks like a bug in XCode, I'm also getting this problem. Commented Nov 29, 2009 at 13:44

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Defining _GLIBCXX_FULLY_DYNAMIC_STRING=1 everywhere in my project solved it for me. Thanks!
1

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;
}

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.