Hello guys, i've been stucking on this for over an hour and i tried to use other STL like vector, but it's still the same question here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// the assign part
list<string> op;
op.push_back("A");
op.push_back("B");
//--------------------
// the problem part
for (list<string>::iterator it=op.begin();
it!=op.end();
++it)
{
string s=*it; // <=program crashed when execute this line.
// some other codes follow...
}
and i tried to use this:
cout << *it;
it works perfectly fine, so please help what exactly is happening?
This looks fine to me, my compiler (cygwin g++ 4.7.2) doesn't complain. What version compiler are you using?
Maybe the clue is in the code you haven't posted, like maybe trying to access s outside your for loop...
@tipaye
Thanks for your reply, it turns out that the problem is that I put the "s" in debug watch window, and it seems the debugger was trying to access "s" when it's not "ready", and cause the crash.
I tried this instead of add a watch on the watch window:
1 2 3 4 5 6 7 8
for (list<string>::iterator it=op.begin();
it!=op.end();
++it)
{
string s=*it; // no crash now
cout << s << endl;
// some other codes follow...
}
and it don't crash.
@ne555
Though your answer was fairly short, it really helps. I checked the link and try to short my code while mantain the crash happens, suddenly on a try, I notice the difference above. Thanks!