Issue Details (XML | Word | Printable)

Key: STDCXX-635
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Farid Zaripov
Reporter: Farid Zaripov
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
C++ Standard Library

std::deque::swap invalidates begin() and end() iterators on empty container

Created: 01/Nov/07 08:01 PM   Updated: 17/Apr/08 09:32 AM
Return to search
Component/s: 23. Containers
Affects Version/s: 4.2.0
Fix Version/s: 4.2.1

Time Tracking:
Original Estimate: 2h
Original Estimate - 2h
Remaining Estimate: 0h
Time Spent - 2h
Time Spent: 2h
Time Spent - 2h

Environment: All
Issue Links:
Dependants
 
Reference

Severity: Incorrect Behavior
Resolution Date: 16/Mar/08 04:58 AM


 Description  « Hide
The program below aborts on assert.

#include <deque>
#include <cassert>

int main ()
{
typedef std::deque<int> Deque;
typedef Deque::iterator Iter;

Deque d1, d2;

Iter iters [2][2] = {
{ d1.begin (), d1.end () },

{ d2.begin (), d2.end () }

};

d1.swap (d2);

assert (d2.begin () == iters [0][0]);
assert (d2.end ()== iters [0][1]);
assert (d1.begin () == iters [1][0]);
assert (d1.end ()== iters [1][1]);

return 0;
}



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Farid Zaripov added a comment - 02/Nov/07 02:47 PM
The addition information can be found in this thread: http://www.mail-archive.com/stdcxx-dev@incubator.apache.org/msg03868.html

Farid Zaripov added a comment - 02/Nov/07 02:47 PM - edited
Personally, I don't think that this is a bug.

After swap operation the d1.begin() == d1.end() and they are valid end iterators for the d1 (d2.begin() == d2.end() and they are valid end iterators for the d2). The only thing that was changed is the binary representation of the iterators, so that operator==(d1.begin() before swap, d2.begin() after swap) returns false.

Just for the information: the Dinkumware STL has the same behavior even on non-empty deque's, because its std::deque::iterator has member deque*_Mycont, so the before swap d1.begin()._Mycont == &d1; after swap d2.begin()._Mycont == &d2; and as a result d1.begin() != d2.begin() in any case.



Farid Zaripov added a comment - 16/Mar/08 04:58 AM - edited
Fixed thus: http://svn.apache.org/viewvc?rev=637539&view=rev

The _C_cur data member of the deque iterator equal to 0 if and only if the deque is empty.
So if one of the iterators passed as arguments to operator-() is iterator of the empty deque, we can return the difference between _C_cur members of these iterators. So if both of them are iterators from the same deque - the result == 0 is valid. If these iterators are from different deque's, the result is undefined, but standard says nothing about result of subtracting the iterators of the different containers. Also if both of these iterators are iterators from different empty deque's, the result will be 0, but this result is still meets the standard requirements (0 value is part of undefined values range).


Farid Zaripov added a comment - 16/Mar/08 04:59 AM
Issue will be closed after successfull nightly testing and merging in 4.2.x branch.

Farid Zaripov added a comment - 17/Apr/08 09:32 AM