I must have not been clear enough.
A C++ string will be, at heart, an array of characters with a null termination. That null termination does not count as part of the string's user-accessible data, in that if you ask the string what size it is, the size will not include the null termination. To access it directly using the [] notation, you'd have to access stringobject[size+1] which I believe the Microsoft debug runtime stomps all over (based on what your error was). (EDIT: SEE CORRECTION BELOW, SHOULD READ ...to access stringobject[size] which I...)
There has to be null termination there, because when you use the string::c_str() function, you get back a pointer to the first element, and you can treat that just like an old-school C-string with null-termination. This kind of old-school access is not stomped on by the MS debug runtime.
There is no argument; there
is an array of char buried in the std::string, it
will have a 0 value on the end of it, you should
not be directly accessing that 0 value with the [] notation applied directly to the string object because the MS debug runtime doesn't like it.
Was that what you were trying to demonstrate? |
Yes. Some other runtimes are very happy with it. I suspect that if you run it not in debug, your system will also be happy with it.