1

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?

4
  • I also have tried adding another class iterator between continue and operations as tmpItr3 = tmpItr. In the cases, where I get segmentation fault i.e. tmpItr is pointing to list.end(), tmpItr3 does not. Which suggests the while breaking the loop, tmpItr changed Commented Sep 22, 2015 at 16:29
  • This is unrelated to goto, and it shouldn’t generally be a problem if/when your iterator equals list.end(). Commented Sep 22, 2015 at 16:32
  • What's the point of comparing two iterators to different lists??? Commented Sep 22, 2015 at 16:32
  • The list of iterator holds pointer to few elements which I need to use frequently (and they change once in a while) .. So instead of searching through the whole list, I minimize the calculations by doing so !! Commented Sep 23, 2015 at 19:15

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, goto is pretty bad — but I’m not convinced this solution is any better, especially considering the tools C++11 now offers us (here, for instance, we could handily use a lambda to nest the loops into, and then early exit out of that.
For nested loops, I think goto is not too bad .. is it?
0

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:

1 Comment

I figured it out .. you are correct .. I was by mistake doing calculation using a not existing vector element. Unlike array, I guess vector holds additional spaces and which is why I was not getting segmentation fault at that location but when calculation get out of loops (without going to goto) and the iterator pointing to non-existing element .. Thank you for your comments :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.