loop thru a STL list causes an infinite loop

A

Allerdyce.John

Hi,

I have this piece of code which loops thru a STL list, but that causs
an infinite loop.

bool Executer::group(MyList& bl, ResultList & grl) {

for (ExecuterList::iterator i = _executerList.begin(); i !=
_executerList.end(); i++) {

Executer* bg = (*i);
bg->group(bl, grl);
}
return true;
}

ExecuterList: is a type 'list<Executer*>'

I am able to iterator the loop once (i.e. I did not stuck inside the
group() of the first executer).
and that list only has 1 item, so I should exit the for() loop after
the group() of the executer has returned.

But it does not, it just stuck there and CPU goes up to 100%.

Any idea is appreciated.
 
D

Dakka

Hi,

I have this piece of code which loops thru a STL list, but that causs
an infinite loop.

bool Executer::group(MyList& bl, ResultList & grl) {

for (ExecuterList::iterator i = _executerList.begin(); i !=
_executerList.end(); i++) {

Executer* bg = (*i);
bg->group(bl, grl);
}
return true;
}

ExecuterList: is a type 'list<Executer*>'

I am able to iterator the loop once (i.e. I did not stuck inside the
group() of the first executer).
and that list only has 1 item, so I should exit the for() loop after
the group() of the executer has returned.

But it does not, it just stuck there and CPU goes up to 100%.

Any idea is appreciated.
You need to post enough code to compile. What does bg->group(...) do?
etc. At a guess, your iterator has been invalidated.
--dakka
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Hi,

I have this piece of code which loops thru a STL list, but that causs
an infinite loop.

bool Executer::group(MyList& bl, ResultList & grl) {

for (ExecuterList::iterator i = _executerList.begin(); i !=
_executerList.end(); i++) {

Executer* bg = (*i);
bg->group(bl, grl);
}
return true;
}

ExecuterList: is a type 'list<Executer*>'

I am able to iterator the loop once (i.e. I did not stuck inside the
group() of the first executer).
and that list only has 1 item, so I should exit the for() loop after
the group() of the executer has returned.

But it does not, it just stuck there and CPU goes up to 100%.

Any idea is appreciated.

Are you sure the program does not hang in `bg->group(bl, grl);'?
Comment it out or replace it with an output statement.

Stephan
 
B

Ben Radford

Hi,

I have this piece of code which loops thru a STL list, but that causs
an infinite loop.

bool Executer::group(MyList& bl, ResultList & grl) {

for (ExecuterList::iterator i = _executerList.begin(); i !=
_executerList.end(); i++) {

Executer* bg = (*i);
bg->group(bl, grl);

You're recursively calling the Executer::group function without doing
any work in bl. For a loop to terminate you need to make progress each
step. In this case you need to do something like removing the head of
the 'bl' list so that it gets smaller each time. I'm surprised it goes
into an infinite loop at all - I would have thought it would seg fault
when the stack overflowed.
}
return true;
}

ExecuterList: is a type 'list<Executer*>'

I am able to iterator the loop once (i.e. I did not stuck inside the
group() of the first executer).
and that list only has 1 item, so I should exit the for() loop after
the group() of the executer has returned.

But it does not, it just stuck there and CPU goes up to 100%.

Any idea is appreciated.

I don't really understand what this code is trying to achieve though I
may have missed something. The fact that you haven't mentioned it seg
faulting makes me wonder if it is recursive. If it is though I would
advise you to refactor it. Recursion has a nasty habit of blowing up the
stack, at least if you don't strictly limit the number of times you recurse.
 
B

Ben Radford

Ben said:
You're recursively calling the Executer::group function without doing
any work in bl. For a loop to terminate you need to make progress each
step. In this case you need to do something like removing the head of
the 'bl' list so that it gets smaller each time. I'm surprised it goes
into an infinite loop at all - I would have thought it would seg fault
when the stack overflowed.


I don't really understand what this code is trying to achieve though I
may have missed something. The fact that you haven't mentioned it seg
faulting makes me wonder if it is recursive. If it is though I would
advise you to refactor it. Recursion has a nasty habit of blowing up the
stack, at least if you don't strictly limit the number of times you
recurse.

I just read a little closer and saw you're comments about the size of
the list - my apologies for misunderstanding you. I still can't see the
problem though, you need to post more code/information. What structure
are the Executer's stored in? It looks like a fibonaci tree.
 
B

benben

Hi,

I have this piece of code which loops thru a STL list, but that causs
an infinite loop.

bool Executer::group(MyList& bl, ResultList & grl) {

for (ExecuterList::iterator i = _executerList.begin(); i !=
_executerList.end(); i++) {

Executer* bg = (*i);
bg->group(bl, grl);

// Just for extra caution the above line should be

if (bg != this)
bg->group(bl, grl);

}
return true;
}

ExecuterList: is a type 'list<Executer*>'

I am able to iterator the loop once (i.e. I did not stuck inside the
group() of the first executer).
and that list only has 1 item, so I should exit the for() loop after
the group() of the executer has returned.

But it does not, it just stuck there and CPU goes up to 100%.

Any idea is appreciated.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top