views:

484

answers:

6

As the title asks.

My understanding of a deque was that it allocated "blocks". I don't see how allocating more space invalidates iterators, and if anything, one would think that a deque's iterators would have more guarantees than a vector's, not less.

+2  A: 

Deque is typically implemented as a circular buffer. If it needs to allocate more space, it needs to copy items from the old buffer to the new one, invalidating any pointers to the old buffer.

laalto
I do not think that this is true, especially if references are to remain valid after a resize.
michalmocny
+2  A: 

Even when you are allocating in chunks, an insert will cause that particular chunk to be reallocated if there isn't enough space (as is the case with vectors).

dirkgently
+5  A: 

The key thing is not to make any assumptions just treat the iterator as if it will be invalidated.

Even if it works fine now, a later version of the compiler or the compiler for a different platform might come along and break your code. Alternatively, a colleague might come along and decide to turn your deque into a vector or linked list.

Tom Leys
+4  A: 

The C++ standard doesn't specify how deque is implemented. It isn't required to allocate new space by allocating a new chunk and chaining it on to the previous ones, all that's required is that insertion at each end be amortized constant time.

So, while it's easy to see how to implement deque such that it gives the guarantee you want, that's not the only way to do it.

Steve Jessop
+3  A: 

What's more interesting is that push_back and push_front will not invalidate any references to a deque's elements. Only iterators are to be assumed invalid.

The standard, to my knowledge, doesn't state why. However if an iterator were implemented that was aware of its immediate neighbors - as a list is - that iterator would become invalid if it pointed to an element that was both at the edge of the deque and the edge of a block.

Shmoopty
Another reason i think is that the iterator could be implemented by keeping an index of an element. If something is inserted at the begin/end of the deque, that index is now not valid anymore (off-by-one), although any pointer/reference to that element it was pointing to is still valid, since no reallocation happened, of course). Same when we keep in index into an array of block-pointers (as i guessed in my comment on the question).
Johannes Schaub - litb
+1  A: 

Because the standard says it can. It does not mandate that deque be implemented as a list of chunks. It mandates a particular interface with particular pre and post conditions and particular algorithmic complexity minimums.

Implementors are free to implement the thing in whatever way they choose, so long as it meets all of those requirements. A sensible implementation might use lists of chunks, or it might use some other technique with different trade-offs.

It's probably impossible to say that one technique is strictly better than another for all users in all situations. Which is why the standard gives implementors some freedom to choose.

janks