std::string
hey there,
i seem to have trouble with std::string
whenever i use a string that has a space in it my program crashes
is there anything i have to consider when i have a longer string that has spaces in it?
whitespace is treated special by things like >> (in default mode reading stops at whitespace - but this can be set different ways). for instance
cin >> name;
would NOT get a name like "Tom Jones";
cin >> name;
would NOT get a name like "Tom Jones";
Quote:Original post by Rozik
hey there,
i seem to have trouble with std::string
whenever i use a string that has a space in it my program crashes
is there anything i have to consider when i have a longer string that has spaces in it?
Any code? How does it "crash"? Does it not do what you want it to? Does it display some kinda of message?
How are you using the string?
If you are tying to read a string with spaces from stdin, use
string s;
getline( cin, s );
That should do what you want. If this was not your problem, we will need more details.
string s;
getline( cin, s );
That should do what you want. If this was not your problem, we will need more details.
i am useing the string with dll
and i am getting an error in the dbgheap.c
i just read a few things and i guess strings and dll's are a bad idea
i guess i am going to try and replace my strings with char*
and i am getting an error in the dbgheap.c
i just read a few things and i guess strings and dll's are a bad idea
i guess i am going to try and replace my strings with char*
It's probably just coincidence that it crashes if you use spaces.
It's a bad idea to pass STL objects across a DLL boundary, unless you're linking to the DLL version of the CRT. The reason is that the memory is allocated in different heaps, so you can't add / remove from the objects easily.
You can't allocate memory in a DLL and free it in the EXE, or vice-versa.
Anyway, if you link to the DLL version of the CRT, it's not a problem.
It's a bad idea to pass STL objects across a DLL boundary, unless you're linking to the DLL version of the CRT. The reason is that the memory is allocated in different heaps, so you can't add / remove from the objects easily.
You can't allocate memory in a DLL and free it in the EXE, or vice-versa.
Anyway, if you link to the DLL version of the CRT, it's not a problem.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement