views:

261

answers:

7

According to C++ standard (3.7.3.2/4) using (not only dereferencing, but also copying, casting, whatever else) an invalid pointer is undefined behavior (in case of doubt also see this question). Now the typical code to traverse an STL containter looks like this:

std::vector<int> toTraverse;
//populate the vector
for( std::vector<int>::iterator it = toTraverse.begin(); it != toTraverse.end(); ++it ) {
    //process( *it );
}

std::vector::end() is an iterator onto the hypothetic element beyond the last element of the containter. There's no element there, therefore using a pointer through that iterator is undefined behavior.

Now how does the != end() work then? I mean in order to do the comparison an iterator needs to be constructed wrapping an invalid address and then that invalid address will have to be used in a comparison which again is undefined behavior. Is such comparison legal and why?

+3  A: 

Huh? There's no rule that says that iterators need to be implemented using nothing but a pointer.

It could have a boolean flag in there, which gets set when the increment operation sees that it passes the end of the valid data, for instance.

unwind
+15  A: 

The only requirement for end() is that ++(--end()) == end(). The end() could simply be a special state the iterator is in. There is no reason the end() iterator has to correspond to a pointer of any kind.

Besides, even if it were a pointer, comparing two pointers doesn't require any sort of dereference anyway. Consider the following:

char[5] a = {'a', 'b', 'c', 'd', 'e'};
char* end = a+5;
for (char* it = a; it != a+5; ++it);

That code will work just fine, and it mirrors your vector code.

Nick Lewis
That says is better than my answer. +1 from me.
sbi
@Nick Lewis: I won't argue against other points, but the Standard says that even using an invalid pointer is UB, so `char* end = a+5;` is UB.
sharptooth
@sharptooth: One past end of the array is **not** an invalid pointer.
UncleBens
@sharptooth: Just because the standard says so.
sbi
Nick, I think you mean `++(--end()) == end()`, because for example `end()-- == end()` also evaluates to true since the postfix form returns the unmodified original.
FredOverflow
@FredOverflow: You're absolutely correct, I've fixed the code. It's actually incorrect for another reason as well: `end()--` returns `end()`, so it then tries to increment `end()` which is not allowed.
Nick Lewis
+1  A: 

The implementation of a standard library's container's end() iterator is, well, implementation-defined, so the implementation can play tricks it knows the platform to support.
If you implemented your own iterators, you can do whatever you want - so long as it is standard-conform. For example, your iterator, if storing a pointer, could store a NULL pointer to indicate an end iterator. Or it could contain a boolean flag or whatnot.

sbi
No tricks required - one past the last element is a valid pointer that can't be dereferenced.
Joe Gauterin
@Joe: I didn't say that tricks were _required_. I said the implementation _can_ play tricks. (And try to have a one-past-the-end iterator for a list.) So I'm not sure what the down-vote is for.
sbi
The question is about why a pointer past the end of an array can be legally used, your answer implies that `end()` is only valid because of implementation defined tricks.
Joe Gauterin
@Joe: I read the question as how implementing `end()` could be legal __for any container__ (and _not_ just for `std::vector`). I didn't even consider `std::vector` to be worth discussing, because I (wrongly) assumed sharptooth to know that a pointer one-past-the-array is allowed. Yet even if it wasn't, an implementation could always allocate room for one more object and use its address for `end()` - which is why I believe the one-past-the-array rule is there _just for arrays__, not for STL containers. Therefor my answer is about any STL container, not `std::vector`.
sbi
+1  A: 

Besides what was already said (iterators need not be pointers), I'd like to point out the rule you cite

According to C++ standard (3.7.3.2/4) using (not only dereferencing, but also copying, casting, whatever else) an invalid pointer is undefined behavior

wouldn't apply to end() iterator anyway. Basically, when you have an array, all the pointers to its elements, plus one pointer past-the-end, plus one pointer before the start of the array, are valid. That means:

int arr[5];
int *p=0;
p==arr+4; // OK
p==arr+5; // past-the-end, but OK
p==arr-1; // also OK
p==arr+123456; // not OK, according to your rule
jpalecek
Why specifically are "before the first" and "beyond the last" pointers valid?
sharptooth
`p==arr-1;` invokes undefined behaviour ("If both the pointeroperand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.")
Joe Gauterin
+1  A: 

Simple. Iterators aren't (necessarily) pointers.

They have some similarities (i.e. you can dereference them), but that's about it.

Roddy
+4  A: 

One past the end is not an invalid value (neither with regular arrays or iterators). You can't dereference it but it can be used for comparisons.

std::vector<X>::iterator it;

This is a singular iterator. You can only assign a valid iterator to it.

std::vector<X>::iterator it = vec.end();

This is a perfectly valid iterator. You can't dereference it but you can use it for comparisons and decrement it (assuming the container has a sufficient size).

UncleBens
Why is "one past the end" valid exactly?
sharptooth
Section 6.5.6.8 of the C standard explicitly allows it.
Joe Gauterin
@sharptooth: The standard talks about the validity of comparing the address of one past the end of arrays in many places. Imagine if this weren't the case - you would not be able to use != to detect the end of arrays when looping, copying, etc which would be very tedious. It is invalid to dereference one past the end though.
markh44
I think another good reason why *one past the end* was defined to be valid is that this is greatly simplifying pointer-arithmetic with arrays. If it wasn't valid, you'd have to `last-first+1` to get the size of an array. Just a guess though.
Space_C0wb0y
+9  A: 

You're right that an invalid pointer can't be used, but you're wrong that a pointer to an element one past the last element in an array is an invalid pointer - it's valid.

The C standard, section 6.5.6.8 says that it's well defined and valid:

...if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object...

but cannot be dereferenced:

...if the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated...

Joe Gauterin
The last quote is not true about C++. If you know that after the array another object of the element type resides (as in a multidim array) you *can* dereference it.
Johannes Schaub - litb
Do you have a reference for that and is it just valid in C++ but not C?
Joe Gauterin
@Joe it's valid (not UB) in C++ and UB in C, yes. But only if there is indeed an object at that position. See `5.7/5` and `3.9.2/3`.
Johannes Schaub - litb