mardi 22 septembre 2015

Issue using for loop and goto statement

I am using C++11 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 segmentation fault. Does goto statement somehow does post increment as well here?

I really appreciate the help. I have been just frustrated with this.

Aucun commentaire:

Enregistrer un commentaire