Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > loop thru a STL list causes an infinite loop

Reply
Thread Tools

loop thru a STL list causes an infinite loop

 
 
Allerdyce.John@gmail.com
Guest
Posts: n/a
 
      01-30-2006
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.

 
Reply With Quote
 
 
 
 
Dakka
Guest
Posts: n/a
 
      01-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-30-2006
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
Ben Radford
Guest
Posts: n/a
 
      01-30-2006
(E-Mail Removed) wrote:
> 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.

--
Ben Radford
"Why is it drug addicts and computer aficionados are both called users?"
 
Reply With Quote
 
Ben Radford
Guest
Posts: n/a
 
      01-30-2006
Ben Radford wrote:
> (E-Mail Removed) wrote:
>
>> 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.
>


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.

--
Ben Radford
"Why is it drug addicts and computer aficionados are both called users?"
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      01-31-2006
(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RDP thru Cisco VPN client and thru 501 Failure curttampa@gmail.com Cisco 21 08-26-2008 03:11 PM
infinite stl list loop boguz C++ 4 10-05-2007 11:38 AM
Can't access Web Service thru DLL (but can thru Windows App) THTB ASP .Net Web Services 0 05-17-2007 06:43 PM
STL string find_first_of ( infinite loop ) scottys0 C++ 2 03-04-2006 10:14 PM
Walking thru a STL list and remove element at the same time? Allerdyce.John@gmail.com C++ 5 02-08-2006 11:51 AM



Advertisments