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.
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.
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.
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