2
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v(10,0); vector<int>::iterator ff = v.begin(); v.assign(3, 11); cout << *ff << endl; cin.get(); return 0; return 0; } 

guess:

something in compiler wrong? something i don't know?

details:

when i see assign that the function of vector in c++ api. by chance i want know allocated storage space in vector and whether can use iterator as pointer. so i write this . but it wrong . i think maybe when in call assign it reallocation memory. but i google it .it said

"This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity."

obvious the capabilities is big so it should not reallocation.i am crazy ,and i try devc++ and it good .why?

6
  • 8
    v.assign() invalidates your iterator causing undefined behaviour. Commented Apr 24, 2015 at 12:31
  • See stackoverflow.com/questions/16904454/… Commented Apr 24, 2015 at 12:35
  • what? invalidates ...T_T alright...thank you.. Commented Apr 24, 2015 at 12:35
  • iterators can be typedefed to pointers(as per standard), and if your vector causes reallocation, it has no idea how many iterators are there(this would be really bad idea for the overhead + very complex reallocation time-wise), so it basically doest care. So lets have an example: iterator's begin was 0x00AA, and when you inserted into it, it became 0x0A00, but your iterator still points to 0x00AA, which is now defined as "free for grabs"(deleted), and so UB happens Commented Apr 24, 2015 at 12:37
  • In the C++ standard library it is specified the various scenarios in which iterators get invalidated, and it is different for different containers. If you have a certain need you can choose a different container with different (stronger?) invalidation guarantees. Commented Apr 24, 2015 at 12:41

1 Answer 1

2

This is due to undefined behavior when you use the iterator after the call to v.assign() as assign invalidates iterators and so using an iterator after that call is a bad idea.

Interestingly VS does reuse the same underlying memory after the call to assign (its still got the same address with capacity 10 but new size of 3) but its got a feature called Debug Iterators. When this feature is on, as it is by default for a Debug build, it stores a list of valid iterators and so knows that your iterator has been invalidated and tells you nicely. In a faster Release build it doesn't run these checks so it has undefined behavior but happens to print out the right values.

A compiler with less sophisticated iterator debugging machinery wont do this and you get undefined behavior (which manifests itself in the most scary way - by doing exactly what you expect)

If you happened to store a pointer to the first element then even Debug Iterators wont help you are you'll probably get the value you expect printed but its still undefined behavior!

Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much!!! i try to release and it really run.so it can use but when use assign vs flag the befoer iterator invalidates then i use the invalidates iterator vs alert wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.