473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

loop thru a STL list causes an infinite loop

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 '06 #1
5 3205
Al************@gmail.com 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
Jan 30 '06 #2
Al************@gmail.com 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

Jan 30 '06 #3
Al************@gmail.com 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?"
Jan 30 '06 #4
Ben Radford wrote:
Al************@gmail.com 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?"
Jan 30 '06 #5
Al************@gmail.com 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
Jan 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Les Juby | last post by:
I have an application which loops through several thousand subscriber records, compares each subscriber's requirements to a database of businesses for sale, and then prepares and mails off an email...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
1
by: Bob | last post by:
The below regular express can sometimes cause an infinite loop. Anyone know what could cause this? string RegexSig = @"To:*\s*(?<address>\S+@\S+)*(\s|\S)*did not reach the following recipient"; ...
3
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Here is my loop and it runs fine: ---------------------------------------------------- sSQL = "SELECT * FROM STORE_ITEMS" Set DataRec = DB.execute(sSQL) if not DataRec.EOF then do while not...
7
by: Curious | last post by:
Hi, I have a C# class that contains a property defined as follows: public bool bRunTwice { get { return bRunTwice; } set { bRunTwice = value; } }
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
5
by: david | last post by:
I'm developing a program that runs using an asyncore loop. Right now I can adequately terminate it using Control-C, but as things get refined I need a better way to stop it. I've developed...
0
by: William Johnston | last post by:
Hi, A support technician proposed a workaround to black images created for thumbnails. The code to create a thumbnail is below: //passes results to Response.OutputStream private void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.