473,659 Members | 2,540 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.b egin(); i !=
_executerList.e nd(); 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 3217
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.b egin(); i !=
_executerList.e nd(); 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.b egin(); i !=
_executerList.e nd(); 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.b egin(); i !=
_executerList.e nd(); 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.b egin(); i !=
_executerList.e nd(); 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 misunderstandin g 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.b egin(); i !=
_executerList.e nd(); 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
3167
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 containing info from the database that the client is interested in. Problem is that the ASP file exits as soon as it has sent about 50 or so records to the Mercury/32 mail server on the local Ethernet. No error message or program execution...
43
5575
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 fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
13
4113
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 dynamic array of characters int length; //number of characters } String;
1
2542
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"; Also shouldn't the framework have some type of loop protection? This will run forever. Bob
3
2693
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 DataRec.EOF SKU = trim (DataRec("SKU")) ITEM_ID = trim(DataRec("ITEM_ID")) ...
7
2122
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
4320
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
6639
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 another program that executes it as a child process using popen2.Popen4(). I was attempting to use signals to stop it (using os.kill()) but I keep running into a problem where sending the signal causes an infinite loop of printing the message...
0
1431
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 CreateThumbnail(string url) { try
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.