Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > infinite loop

Reply
Thread Tools

infinite loop

 
 
Keith Thompson
Guest
Posts: n/a
 
      01-28-2009
Eric Sosman <(E-Mail Removed)> writes:
[...]
> All of them consume more cycles than your machine can execute.
> Aleph-null is aleph-null is aleph-null, and the Universe will rot
> away before any of these loops completes.
>
> (Unless you've got a DeathStation 9000, of course. The DS9K
> is so fast it can complete a well-optimized infinite loop in twenty
> seconds.)


Given this:

...
for (; { }
puts("Done!");

it can legally complete the loop as long as it never gets around to
executing the puts call.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Stephen Sprunk
Guest
Posts: n/a
 
      01-28-2009
karthikbalaguru wrote:
> On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
>> kenny wrote:
>>> i want to write infinite loop
>>> 1. can do in c language ?

>> while (1) {} /* one way */
>>
>> do {} while(1); /* another way */
>>
>> for (; {} /* a third way */
>>
>> foo: goto foo; /* a fourth way */
>>

>
> But, why does C provide some many
> ways of infinite loop ?
>
> Which one of the above methods consume
> less cycles ?


There is no definite answer, because the Standard does not promise
anything at all in terms of performance; you'd need to ask someone
familiar with the specific implementation you're using.

However, with any modern optimizing compiler, all four of the above
constructs will likely translate to the exact same machine instructions,
so there will be no performance difference at all.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Isaac Jaffe
 
Reply With Quote
 
 
 
 
Kaz Kylheku
Guest
Posts: n/a
 
      01-28-2009
On 2009-01-28, Keith Thompson <(E-Mail Removed)> wrote:
> karthikbalaguru <(E-Mail Removed)> writes:
>> On Jan 24, 5:59 am, Martin Ambuhl <(E-Mail Removed)> wrote:
>>> kenny wrote:
>>> > i want to write infinite loop
>>>
>>> > 1. can do in c language ?
>>>
>>> while (1) {} /* one way */
>>>
>>> do {} while(1); /* another way */
>>>
>>> for (; {} /* a third way */
>>>
>>> foo: goto foo; /* a fourth way */
>>>

>>
>> But, why does C provide some many
>> ways of infinite loop ?

>
> C doesn't provide any construct that's specifically for infinite
> loops.


The for construct supports infinite looping; it's the only construct in whose
syntax the guard condition can be omitted, which indicates that the loop is
body is unconditionally executed.

I.e. the unconditional execution is indicated by the true lack of a condition,
rather than simulated by the use of some condition which is always true.

This constitutes special syntax for the purpose of writing unconditional loops,
which are infinite loops if no other measures are taken to break out of them.
 
Reply With Quote
 
Mark L Pappin
Guest
Posts: n/a
 
      01-28-2009
Keith Thompson <(E-Mail Removed)> writes:

> Eric Sosman <(E-Mail Removed)> writes:
>> The DS9K is so fast it can complete a well-optimized infinite loop
>> in twenty seconds.


> for (; { }
> puts("Done!");
>
> it can legally complete the loop as long as it never gets around to
> executing the puts call.


A few years back while wearing my embedded-compiler-vendor technical-
support hat, I had a customer complain that our compiler "completely
removed all [his] program code" following a single-character typo.
His source was approximately

main() {
init();
while (1) ;
{
program();
code();
}
}

but the typo was exactly as shown.

I can't recall how, or even if, I convinced him that this was working
perfectly and that to do otherwise would have been R.O.N.G.

He wanted DWIM, but that hasn't made it into the Standard yet.

mlp
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-28-2009
karthikbalaguru wrote:
>

.... snip ...
>
> But, why does C provide some many ways of infinite loop ?
>
> Which one of the above methods consume less cycles ?


Er, all infinite loop consume infinite cycles. This assumes the
loops are really infinite. If you wish to conserve cycles, you
don't write infinite loops.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-28-2009
Mark L Pappin wrote:
>

.... snip ...
>
> A few years back while wearing my embedded-compiler-vendor
> technical-support hat, I had a customer complain that our compiler
> "completely removed all [his] program code" following a single-
> character typo. His source was approximately
>
> main() {
> init();
> while (1) ;
> {
> program();
> code();
> }
> }
>
> but the typo was exactly as shown.
>
> I can't recall how, or even if, I convinced him that this was working
> perfectly and that to do otherwise would have been R.O.N.G.
>
> He wanted DWIM, but that hasn't made it into the Standard yet.


To me, this is a reason to encourage formatting as:

while (condition) {
code();
morecode();
}

where it is hard to fail to notice a misplaced semi.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-29-2009
kenny wrote:
>
> i want to write infinite loop
>
> 1. can do in c language ?


Yes.

> 2. how is useful ?


Not if you are successful.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
 
Reply With Quote
 
Mark L Pappin
Guest
Posts: n/a
 
      01-29-2009
CBFalconer <(E-Mail Removed)> writes:

> Mark L Pappin wrote:

[saw customer's typo]
>> while (1) ;
>> {
>> program();
>> code();
>> }


> To me, this is a reason to encourage formatting as:
>
> while (condition) {
> code();
> morecode();
> }
>
> where it is hard to fail to notice a misplaced semi.


Had it been acceptable to force my preferred style on our customers,
that's what I would have done, and why. Wasn't; didn't.

mlp
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      01-29-2009
On 29 Jan 2009 at 3:45, Han from China - Master Troll wrote:
> Oh, wow. Chuck managed a witty comment. The old fart isn't completely
> dull, after all.


Only a one-handed clap, though. Wit really demands a certain degree of
spontaneity, and Chuck blundering in, late to the party as usual, and
coming out with the same "joke" that half a dozen people have already
done to death hours earlier does seem a bit too clumsy to be called
wit...

 
Reply With Quote
 
Nelu
Guest
Posts: n/a
 
      01-29-2009
On Thu, 29 Jan 2009 14:51:40 +1000, Mark L Pappin wrote:

> CBFalconer <(E-Mail Removed)> writes:

<snip>
>> To me, this is a reason to encourage formatting as:
>>
>> while (condition) {
>> code();
>> morecode();
>> }
>>
>> where it is hard to fail to notice a misplaced semi.

>
> Had it been acceptable to force my preferred style on our customers,
> that's what I would have done, and why. Wasn't; didn't.


Are there any formatting/re-styling applications? For example, in
eclipse, I can write in a certain style then reformat with different
style settings.



--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org


 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
infinite loop unexpectly dies Vedran Vukotic Perl 0 03-02-2006 07:43 AM
loop thru a STL list causes an infinite loop Allerdyce.John@gmail.com C++ 5 01-31-2006 03:21 PM
Infinite loop when using Server.Transfer Alexander Bosch ASP .Net 11 11-10-2004 08:53 AM
Infinite loop when using Server.Transfer Alexander Bosch ASP .Net 1 11-01-2004 02:55 AM



Advertisments