loop thru a STL list causes an infinite loop

Discussion in 'C++' started by Allerdyce.John@gmail.com, Jan 30, 2006.

  1. Guest

    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.
     
    , Jan 30, 2006
    #1
    1. Advertising

  2. Dakka Guest

    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
     
    Dakka, Jan 30, 2006
    #2
    1. Advertising

  3. 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
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 30, 2006
    #3
  4. Ben Radford Guest

    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?"
     
    Ben Radford, Jan 30, 2006
    #4
  5. Ben Radford Guest

    Ben Radford wrote:
    > 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?"
     
    Ben Radford, Jan 30, 2006
    #5
  6. benben Guest

    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
     
    benben, Jan 31, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    348
    Richard Herring
    Feb 8, 2006
  2. scottys0
    Replies:
    2
    Views:
    397
    scottys0
    Mar 4, 2006
  3. boguz

    infinite stl list loop

    boguz, Oct 5, 2007, in forum: C++
    Replies:
    4
    Views:
    443
    terminator
    Oct 5, 2007
  4. THTB
    Replies:
    0
    Views:
    197
  5. Max Williams
    Replies:
    3
    Views:
    175
    Robert Klemme
    Jan 6, 2009
Loading...

Share This Page