• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: XCode, cin and string (C++) => troubles :s
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: XCode, cin and string (C++) => troubles :s


  • Subject: Re: XCode, cin and string (C++) => troubles :s
  • From: Arnaud Schoofs <email@hidden>
  • Date: Sun, 14 Feb 2010 14:26:58 +0100

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:

<http://www.cplusplus.com/forum/articles/6046/>



Regards
Andreas
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: XCode, cin and string (C++) => troubles :s
      • From: Andreas Grosam <email@hidden>
  • Prev by Date: Re: XCode, cin and string (C++) => troubles :s
  • Next by Date: Re: XCode, cin and string (C++) => troubles :s
  • Previous by thread: Re: XCode, cin and string (C++) => troubles :s
  • Next by thread: Re: XCode, cin and string (C++) => troubles :s
  • Index(es):
    • Date
    • Thread