While learning c++ I went through the pains of learning what iterators were, and exactly how to use them. Then, I went through the pain of learning what function calls invalidate iterators, and spotting them quickly. I've been following questions lately and have noticed that a very common theme is people not realizing that calling vector::erase() invalidates iterators. This made me wonder if there were a debug implementation of vector that does runtime checking against vector valid-ness?
My initial thoughts were that it would have to be a wrapper around vector that basically tracked all existing iterators, and set an isValid boolean when an action was performed that invalidated them. Then, when dereferencing or incrementing, isValid could be checked.
This would help diagnose more obscure errors where the erase() call is nested (accidentally, or unknowingly) deep within a function call, while that same vector is being iterator over.
Does anyone know of such an implementation, or any reasons why one should not be used?
EDIT: I am using linux and g++.
Microsoft's implementation does some debug checking (ref, and ref). It checks specifically for range, so I'm not sure if the erase() scenario fits or not.
Edit for jalf's contribution:
It appears that GCC (and g++) has similar functionality:
stackoverflow.com/questions/2567997/ and
gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With