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?

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
Cong Wu
  • 33
  • 3
  • 8
    `v.assign()` invalidates your iterator causing undefined behaviour. – Galik Apr 24 '15 at 12:31
  • See http://stackoverflow.com/questions/16904454/what-is-iterator-invalidation – chris Apr 24 '15 at 12:35
  • what? invalidates ...T_T alright...thank you.. – Cong Wu Apr 24 '15 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 – Creris Apr 24 '15 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. – Mike Vine Apr 24 '15 at 12:41
  • iterator p=xx.begin() it point to 0x00AA and now it is the begin of vector. i insert(3),so the begin change.begin in 0x00A0. but because vector is continuous so i should get the value in 0x00AA ?what the problem? – Cong Wu Apr 24 '15 at 12:50

1 Answers1

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!

Mike Vine
  • 9,468
  • 25
  • 44
  • 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. – Cong Wu Apr 24 '15 at 12:58