I have following code, that fails due to read access violation
:
#include <vector>
using namespace std;
vector<int>::iterator myIterator;
void foo(vector<int> vec) {
myIterator = vec.begin();
}
int main()
{
foo({ 10, 20, 30, 40 });
*myIterator; // Here it fails.
return 0;
}
After some debugging, I found, that myIterator
becomes invalid, when the function foo()
is completed.
So what am I doing wrong?