Thanks for your help. But it doesn't really help me.
Your're right, i've made a big mistake with the char not initialized (but it was - don't know how ^^ - working). But it's not the source of my problem. I've read the interesting link you gave me but it doesn't help.
Indeed, i had already try using getline() but it didn't work either. I just tried again this code : using namespace std; int main (int argc, char * const argv[]) { string input = ""; cout << "type something :"; getline(cin, input); cout << "you entered : " << input << endl << endl; return 0; } It compiles well, but i get this error : type something :test helloworld(1777) malloc: *** error for object 0x512c: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug you entered : test
Notice that if i replace : string input = ""; by string input = "blabla"; It's fully working ...
How can you explain that ? A bug in XCode ? :/
Thanks again for your help !
Le 14 févr. 2010 à 14:01, Andreas Grosam a écrit : On Feb 14, 2010, at 12:38 PM, Arnaud Schoofs wrote: #include <iostream> using namespace std; int main (int argc, char * const argv[]) { string s; cout << "Type something (using string) : "; cin >> s; cout << s << "\n\n"; char* c; cout << "Type something (using char*) : "; cin >> c; cout << c << "\n\n"; return 0; }
Your pointer to char c is not initialized, nor does it point to a reasonable buffer. So, cin writes *somewhere* into memory. That it crashes is normal ;)
Anyway, using a pointer to a char as input buffer isn't a reasonable way to go.
Furthermore, the pattern
std::cin >> some_variable
is frequently causing troubles, when the details about an istream are not fully understood. For instance,
std::string str; std::cin >> str;
is completely inappropriate to read a "string" (that is input) from the console.
Please read this article for further information:
Regards Andreas
|