Working Dev C++ code won't work in Visual C++

Pages: 123
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.
Last edited on
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.

*C++98 §21.3.4[lib.string.access]/1, C++11 §21.4.5[string.access]/2
Last edited on
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.
[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]


Oh. I completely misunderstood what Athar was doing with that code.

I had thought that the subscript was itself allocating memory for the string literal, so I figured [10] was "far more than enough".

But indeed, 0-5 in the subscript does not trigger the error.
So.... not too many VC++2010 users around here, ay?
Last edited on
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];
}
Last edited on
@Peter87 indeed, Visual Studio 2010 SP1 conforms to C++03 in that respect (not C++11 though). Nevermind my comment.
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.
Topic archived. No new replies allowed.
Pages: 123