infinite loop

D

Default User

kenny said:
i want to write infinite loop

1. can do in c language ?

That's a philosophical question. You can write one that has no
algorithmic exit to the loop. There's little chance that one would be
truly infinite.
2. how is useful ?

I don't know, you're the one that wants one. Why do you think it would
be useful?




Brian
 
M

Martin Ambuhl

kenny said:
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 */

2. how is useful ?

To keep children amused.
 
E

Eric Sosman

kenny said:
i want to write infinite loop

1. can do in c language ?

You can write it in the C language, but no actual C
implementation will execute it as written. (Ir follows
that all actual C implementations are non-conforming, or
at least that there is no conformance test that could
yield an unequivocal "Yes.")
2. how is useful ?

Only if satisfying your own stated desire is "useful."
 
T

Thad Smith

kenny said:
i want to write infinite loop ....
2. how is useful ?

It's used in many embedded computing applications in which there is one
program that keeps running the same logic as long as the power is on.

It is used in general run-to-termination programs that terminate the
loop from within, typically as a function return or break when some
condition is satisfied:

while (1) {
do_something();
if (error) return -1
do_something_else();
if (different_error) return -2;
if (normal_exit) return 0;
get_ready_to_do_more();
}
 
A

Andrew Smallshaw

i want to write infinite loop

1. can do in c language ?

There are many ways. for (;;) {} is the canonical one, although
I've seen while (1) {} and even a label and goto used to implement
it. I strongly urge for (;;) since it is immediately recognisable
as an infinite loop. With a while (1) in a work in progress it
isn't always clear whether an infinite loop is desired or the 1 is
simply a placeholder for a condition that has yet to be determined.
2. how is useful ?

Any time you want to execute a given section of code an indefinite
number of times. It should be understood that no infinite loop is
really infinite: it is something of a misnomer. Instead an "infinite"
loop repeats until either the function or the program terminates.

Another reason that is sometimes overlooked is to take the loop
control inside the loop body. This uses the same for (;;) structure
but of course is even less of an infinite loop than the first case.
Perhaps the most common case for this is when you need to initialise
a number of data structures on a per-iteration basis _before_ you
can test your condition. Do all your initialisation, test the loop
condition and break out if it fails.

The alternative ends up with a loop body performing the second half
of one iteration followed by the first half of the next. This is
messy and difficult to understand, which only gets worse when you
remember you need to initialise for the first iteration before the
loop and clean up after the last one after the loop. Avoid code
that does that at all costs.
 
N

Nate Eldredge

Golden California Girls said:
Rather than while (1) {} which I agree can cause questions for later maintainers
of the code I have seen while (hell_freezes_over) {} to make it obvious the
author intends it to be infinite. I even seen the do {} while
(hell_freezes_over) form.

That's pretty misleading; I would expect the loop to terminate
immediately, since hell is not yet freezing over. Maybe you got it from
a language that has repeat/until. I'd also probably make the condition
a macro and put it in caps.

Here's some other ideas:

while (A_THING_OF_BEAUTY_IS_A_JOY) { }
while (THERE_ARE_STARS_ABOVE_YOU) { }
while (DEATH_AND_TAXES_EXIST) { }
while (!ENGLAND_IS_A_SLAVE) { }
while (CRICKET_MATCH_CONTINUES) { }

;-)
 
K

Keith Thompson

Golden California Girls said:
Rather than while (1) {} which I agree can cause questions for later
maintainers of the code I have seen while (hell_freezes_over) {} to
make it obvious the author intends it to be infinite. I even seen
the do {} while (hell_freezes_over) form.

Yeah, I did that kind of thing myself many years ago. Specifically, I
used something like:

#define EVER ;;
...
for (EVER) {
...
}

It's cute -- and that's not a good thing. The English meaning of
"for (EVER)" is clear enough, but this isn't English, it's C.
The reader must either assume that you got the macro definition
right, or waste time tracking down the macro definition, mentally
expand it, and *then* understand that "for (;;)" is an infinite loop.

If you want your code to be understood, just write "for (;;)" or
"while (1)" and be done with it.
 
K

karthikbalaguru

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 ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

karthikbalaguru said:





Imagine that you are a craftsman in a workshop that contains many
tools. Someone comes in and asks you how many different ways you
can think of to insert a woodscrew into an offcut.

First, you demonstrate how to do this with a manual screwdriver.

Then you do the same thing, this time with an electric screwdriver.

Then you show that it can be done with a hammer, too, albeit not
quite so neatly.

Fourthly, you cut a small recess in the wood with a hammer and
chisel and place a screw into the recess.

Fifthly, you drill a hole wider than the screw and drop the screw
into the hole. (Again, you can do this both manually and
electrically.)

You may be able to think of some other ways to do this, too.

But, says your visitor, WHY does carpentry provide so many ways to
do this?


That's the wrong question - partly because it makes little if any
difference, and partly because the answer would vary from machine
to machine,

Considering a x86 machine, which one will be faster (Less Cycles) ?
Any ideas ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

karthikbalaguru said:





Imagine that you are a craftsman in a workshop that contains many
tools. Someone comes in and asks you how many different ways you
can think of to insert a woodscrew into an offcut.

First, you demonstrate how to do this with a manual screwdriver.

Then you do the same thing, this time with an electric screwdriver.

Then you show that it can be done with a hammer, too, albeit not
quite so neatly.

Fourthly, you cut a small recess in the wood with a hammer and
chisel and place a screw into the recess.

Fifthly, you drill a hole wider than the screw and drop the screw
into the hole. (Again, you can do this both manually and
electrically.)

You may be able to think of some other ways to do this, too.

But, says your visitor, WHY does carpentry provide so many ways to
do this?


That's the wrong question - partly because it makes little if any
difference, and partly because the answer would vary from machine
to machine,

Considering a x86 machine, which one will be faster (Less Cycles) ?
and Which one will consume more memory ?
Any ideas ?

Thx in advans,
Karthik Balaguru
 
K

karthikbalaguru

karthikbalaguru said:


karthikbalaguru said:
Which one of the above methods [of programming an infinite
loop] consume less cycles ?
That's the wrong question - partly because it makes little if any
difference, and partly because the answer would vary from machine
to machine,
Considering a x86 machine, which one will be faster (Less Cycles)
? Any ideas ?

It's still the wrong question.

Consider this: either the loop body will be doing a lot of work (in
which case the tiny cost of looping, *however* you do it, will be
insignificant), or the loop body will not be doing a lot of work
(in which case the overhead doesn't matter anyway, because there's
plenty of capacity).

Thx for that info !!
But, i am really interested to know considering
the loop body does not do anything. (It is just an infinite loop
without body.)

I am intested to know this because various forms of
programming an infinite loop is supported .

Also, are there any specific scenarios in which
those methods should be used ?

Any ideas ?

Thx in advans,
Karthik Balaguru
 
K

Keith Thompson

karthikbalaguru said:
But, why does C provide some many
ways of infinite loop ?

C doesn't provide any construct that's specifically for infinite
loops. It provides a number of distinct looping constructs, each with
its own uses. (You could get by with just one, but the different
forms are more convenient in different circumstances.) And, not
surprisingly, each of the forms of loop can be specialized to form an
infinite loop.
Which one of the above methods consume
less cycles ?

As Richard explained at some length, you're asking the wrong question.
But to answer your question anyway, very probably they all consume
exactly the same number of cycles. A decent compiler will probably
generate exactly the same code for all of them.
 
G

Guest

Considering a x86 machine, which one will be faster (Less Cycles) ?
and Which one will consume more memory ?
Any ideas ?

its been explained to you why this is a bad question.
If you *really* want to know (and I can think of
no good reason) then write a program and look at the assembler
generated. You will find the code very similar in all cases.
 
B

Bartc

karthikbalaguru said:
But, why does C provide some many
ways of infinite loop ?

Why does it provide so many ways of looping?
Which one of the above methods consume
less cycles ?

For an /infinite/ loop, will it really make any difference? Everyone will
still be dead long before.
 
M

Martin Ambuhl

karthikbalaguru said:
But, i am really interested to know considering
the loop body does not do anything. (It is just an infinite loop
without body.)

I am intested to know this because various forms of
programming an infinite loop is supported .

Also, are there any specific scenarios in which
those methods should be used ?

Any ideas ?

This is just silly. If a loop is truly infinite it has an infinite
number of cycles performed in an infinite amount of time. So constuct 1
has infinity[1] cycles / infinity[1] time and construct 2 has
infinity[2] cycles / infinity[2] time.
 
H

Harold Aptroot

(snipped far too long quote)
Considering a x86 machine, which one will be faster (Less Cycles) ?
Any ideas ?

Thx in advans,
Karthik Balaguru

Any real compiler is likely to optimize all those kinds of loops into:

foo: goto foo;

But in case it won't (maybe a very dumb/old compiler), writing that directly
shouldn't result in more than just a jump. There is no guarantee though, the
compiler is free to (for example) insert a whole load of nops between the
label and the goto (or anywhere else). I would trust most C compilers to be
"sane" in addition to "conforming to the C standard" though.
 
E

Eric Sosman

karthikbalaguru said:
But, why does C provide some many
ways of infinite loop ?

Which one of the above methods consume
less cycles ?

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top