Iterator invalidation is what happens when an iterator type (an object supporting the operators ++
, and *
) does not correctly represent the state of the object it is iterating. For example:
int *my_array = new int[15];
int *my_iterator = &my_array[2];
delete[] my_array;
std::for_each(my_iterator, my_iterator + 5, ...); // invalid
That results in undefined behavior because the memory it is pointing to has been reclaimed by the OS.
This is only one scenario, however, and many other things cause an iterator to be 'invalidated', and you must be careful to check the documentation of the objects you are using.