Athar- I thought that too but before I removed the subscript I tried changing the [6] to a [10] and still got it. Definitely shouldn't have been writing out of bounds then?
[10] is way, way out of bounds on something that has only six elements.
With an array of size 6 (let's call it str), the following elements are in bounds:
str[0]
str[1]
str[2]
str[3]
str[4]
str[5]
The following are out of bounds:
str[6]
str[7]
str[8]
str[9]
str[10]
str[11]
str[12]
str[13]
and so on, all the way up to str[infinity]
The following are also out of bounds:
str[-1]
str[-2]
str[-3]
str[-4]
str[-5]
str[-6]
and so on, all the way up to str[-infinity]
The debate goes on as to whether or not you should be allowed to access the null termination element on the char array data section of a std::string, but you definitely can't fiddle around with the element 4 after that.
they figured accessing the '\0' after the string is usually the result of an off-by-one error and rarely intentional. If so, they're right.
If so, they are wrong: accessing a std::string with operator[] (but not with at()) one past the end has always been perfectly legal* C++ as long as the access is read-only.
If so, they are wrong: accessing a std::string with operator[] (but not with at()) one past the end has always been perfectly legal* C++ as long as the access is read-only.
We already established that. That's not the point.
If so, they are wrong: accessing a std::string with operator[] (but not with at()) one past the end has always been perfectly legal* C++ as long as the access is read-only.
C++03 they say
Returns: If pos < size(), returns data()[pos]. Otherwise, if pos == size(), the const version returns charT(). Otherwise, the behavior is undefined.
So only the const version is defined. This should work fine
1 2 3 4 5 6
#include <string>
int main()
{
const std::string str("Hello!");
str[6];
}
dying to let this thread sail off into oblivion at this point, but since it's already at the top just wanted to thank you guys for your time and patience.