Re: vector.erase(iterator iter) will change "iter" or not?
Old Wolf wrote:
On Feb 23, 6:40 am, James Kanze <james.ka...@gmail.com> wrote:
unsigned char before[ sizeof( std::vector<int>::iterator ] ;
memcpy( before, &iter, sizeof( std::vector<int>::iterator ) ) ;
v.erase( iter ) ;
memcmp( before, &iter, sizeof( std::vector<int>::iterator ) ) ;
the memcmp will return a value not equal to 0.
How does that work?
About how you'd expect. The container knows about the iterators
which refer to it, and marks them as invalid whenever it
invalidates them.
I agree that it's legal, but I wouldn't expect it anywhere
except the DS9000; it seems that the implementation, when
faced with vector::erase, would have to go out of its way to
go and change bits in the original 'iter' that the parameter
to vector::erase was copied from.
I'm not sure what you mean by "go out of its way". Every
pre-standard iterator I ever wrote did this. Logically, the
iterator knows about the container, and vice versa.
And trying to use the iterator will cause a core dump.
To be expected when using indeterminate 'values'.
Huh? On my system, I don't get a core dump just because I copy
an invalid pointer (say, after a delete). I do if I copy an
invalid iterator, however. (If, after the erase above, I assign
iter to some other iterator, I get a core dump, with a message
that I've used an invalid iterator.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34