Re: Interesting string.resize behavior
On Jun 21, 7:02 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
v4vijayakumar wrote:
On Jun 21, 9:49 pm, Clark Cox <clarkc...@gmail.com> wrote:
You're going to have to state exactly what it is that you find
interesting about this.
...
string str;
str.resize(5);
str[0] = 't';
str[1] = 'e';
str[2] = 's';
str[3] = 't';
str[4] = '\0';
str += "-test2";
str += "-test3";
cout << str << endl;
...
Well, surprise!
Output is not, "test-test1-test2", but just "test". :)
[ Tried in MS VS 6.0. ]
That's looks and sounds like a buggy compiler or library.
The implementation of std::string in VC++ 6.0 didn't handle '\0'
in std::string correctly. In many cases, in fact, a string with
a '\0' would crash the program.
Have you tried it on any more recent (or just different) one?
This problem isn't present in the current version of the
compiler (at least in the cases where I'd seen it---the code
which didn't work then works now).
Note that a '\0' character in a string can have curious effects
on an output device. You're not allowed to output it to a
stream opened in text mode (like cout), so his results don't
actually prove a bug anywhere. To be sure, he should open a
file in binary mode, output to it, and then verify the contents.
It's quite possible that the phenomena that he is observing has
nothing to do with the bug I mention.
--
James Kanze (GABI Software, from CAI) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34