infinite loop

K

Keith Thompson

Eric Sosman said:
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.
 
S

Stephen Sprunk

karthikbalaguru said:
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
 
K

Kaz Kylheku

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.
 
M

Mark L Pappin

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
 
C

CBFalconer

karthikbalaguru said:
.... 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.
 
C

CBFalconer

Mark said:
.... 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.
 
M

Mark L Pappin

CBFalconer said:
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
 
A

Antoninus Twink

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...
 
N

Nelu

CBFalconer <[email protected]> writes:

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.
 
S

Stephen Sprunk

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

GNU indent is a popular one; it offers a multitude of options that will
format your code according to whatever style you prefer.

In general, such tools are called "pretty printers".

S
 
N

Nelu

GNU indent is a popular one; it offers a multitude of options that will
format your code according to whatever style you prefer.

How could I forget about indent? I actually used it in the past :)
 
C

CBFalconer

Nelu said:
.... snip ...

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

I use indent, from gnu. I have version 2.2.9. This is pretty well
the industry standard reformatter etc. Works only on C, not C++.
 
T

Tim Rentsch

Kaz Kylheku said:
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.

I agree with the observation (that only 'for' allows its guard
condition to be omitted) but draw a different conclusion, namely,
using for(;;) is more prone to misunderstanding (than while(1), for
example), because the special case of an empty guard has to be
remembered separately. Generally speaking, the fewer special
cases that need to be remembered, the better, IMO.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top