Reputation: 13
I am using C++11 std::list
and list iterators. I use nested loops and test a condition and if it succeeds, I use goto
statement to get out. A pseudo code is shown below:
std::list<myclass>::iterator tmpItr;
std::list<myclass>::iterator tmpItr2;
std::list<std::list<myclass>::iterator>::iterator radItr;
double cutoffVal = someVal;
double currVal = 0.0;
for (radItr=radList.begin();radItr!=radList.end();radItr++) {
tmpItr2 = *radItr;
for (tmpItr=pList.begin();tmpItr!=pList.end();tmpItr++) {
if (tmpItr == tmpItr2)
continue;
/* Some Operation Here */
currVal += NewVal;
if (currVal >= cutoffVal)
goto BREAKLOOP;
}
}
BREAKLOOP:
// Use tmpItr and tmpItr2
My problem is that sometime the value of tmpItr
goes to list.end()
and I get a segmentation fault. Does the goto
statement somehow does post increment as well here?
Upvotes: 1
Views: 104
Reputation: 30827
I think your problem occurs when the goto
is not reached:
for (...; radItr != radList.end(); ...) {
for (...; tmpItr != pList.end(); ...) {
if (...)
goto BREAKLOOP;
}
}
// At this point, radItr==radList.end() && tmpItr==pList.end()
BREAKLOOP:
Upvotes: 0
Reputation: 135
I'm not positive it will fix the problem with your iterators, but most people I talk to consider goto's in c++ to be bad code. Why not replace goto with:
(bool)breakout=true;
break;
}
if(breakout)break;
Upvotes: 1