how to do an infinite loop

T

tmp123

Pedro said:
Do not add to the list (unless you're considering an "infinite" computer)
void f(void) { ...; f(); }

inline void f(void) { ...;f(); } ???

or a compiler who knows what means "tail recursion".
 
J

Jordan Abel

#define until while
#define the_cows_come_home 17

do {
... stuff ...
} until (the_cows_come_home);

some alternatives (mix and match)

#define hell_freezes_over (1 != 0)
#define no_dog_has_fleas (EOF < 0)

Heh. It makes more sense to define them as false statements and define
until(x) as while(!(x)) - since that is the semantics of until in
languages that actually have it.
 
J

John F

CBFalconer said:
#define until while
#define the_cows_come_home 17

do {
... stuff ...
} until (the_cows_come_home);

some alternatives (mix and match)

#define hell_freezes_over (1 != 0)
#define no_dog_has_fleas (EOF < 0)

Extraordinarily great idea!!! Had a good laugh...

how about:

#define you_are_old_and_grey (0?(0?0:0):1)
#define you_stop_wondering_about_that_constant 1

:)

John
 
W

William J. Leary Jr.

John F said:
Extraordinarily great idea!!! Had a good laugh...

how about:

#define you_are_old_and_grey (0?(0?0:0):1)
#define you_stop_wondering_about_that_constant 1

I put off mentioning this one, because I was SURE someone would beat me to it.
Maybe someone did and I didn't catch the message.

I've run across this a number of times:

#define ever ;;

for (ever)
{
/* We're in here forever */
}
 
J

John F

William J. Leary Jr. said:
I put off mentioning this one, because I was SURE someone would beat me to
it.
Maybe someone did and I didn't catch the message.

I've run across this a number of times:

#define ever ;;

for (ever)
{
/* We're in here forever */
}

That's great! :)
 
K

Keith Thompson

William J. Leary Jr. said:
I put off mentioning this one, because I was SURE someone would beat
me to it. Maybe someone did and I didn't catch the message.

I've run across this a number of times:

#define ever ;;

for (ever)
{
/* We're in here forever */
}

I've used that myself, many years ago.

It's very clever. In this context, "clever" is a derogatory term.

I appreciate the humor of cleverly obfuscated code, but if you want an
infinite loop in C code that's intended to be maintained, there are
two common ways to do it:

while (1) { ... }
for (;;) { ... }

There is *no* good reason to use anything else.
 
G

Guillaume

William said:
#define ever ;;

for (ever)
{
/* We're in here forever */
}

Macros can be a great tool, but they should not be used, in my opinion,
as syntactic sugar. The base syntax of the language should not be
modified in any way if you want your code to be readable. A 'for'
loop without two visible ';' is a syntax mess. Don't do it.

Besides, I try to stick to the convention of declaring macros in all
uppercase. This way, you can see immediately if you are dealing with a
macro.

Preprocessors are handy but dangerous, as they are not really part
of the language itself. Thus you should avoid abstracting its use
at all costs.
 
J

John F

Guillaume said:
Macros can be a great tool, but they should not be used, in my opinion,
as syntactic sugar. The base syntax of the language should not be
modified in any way if you want your code to be readable. A 'for'
loop without two visible ';' is a syntax mess. Don't do it.

#define ever

for (;ever;)
{
/*code to do*/
}

better?
Besides, I try to stick to the convention of declaring macros in all
uppercase. This way, you can see immediately if you are dealing with a
macro.

Preprocessors are handy but dangerous, as they are not really part
of the language itself. Thus you should avoid abstracting its use
at all costs.

ACK ... It is great for general purpose use but should not be used to "bend"
the language...
 
K

Keith Thompson

John F said:
#define ever

for (;ever;)
{
/*code to do*/
}

better?

No.

If someone reading your code knows C, he knows perfectly well what
"for (;;)" means; he'll have no idea what "ever" means without looking
it up. If he doesn't know C, no amount of preprocessor trickery will
let him understand the code; rather, it will prevent him from learning
good C.

(I don't think you meant to imply that it *is* better, so take this as
an expansion on what you wrote, not a criticism.)

Note that a sufficiently malicious programmer could do something like
this:

#define ever 0

for (;ever;) {
/* this is never executed */
}
 
J

John F

Keith Thompson said:

I just wanted to be a little bit sarcastic.
If someone reading your code knows C, he knows perfectly well what
"for (;;)" means; he'll have no idea what "ever" means without looking
it up. If he doesn't know C, no amount of preprocessor trickery will
let him understand the code; rather, it will prevent him from learning
good C.

Depends on whether he needs to learn it... Some bosses strictly refuse to
learn anything...
(I don't think you meant to imply that it *is* better, so take this as
an expansion on what you wrote, not a criticism.)

I just wanted to point out, that present ";;" don't necessarily imply
readability/maintainability... It can still be very ambiguous, as you have
pointed out correctly:
Note that a sufficiently malicious programmer could do something like
this:

#define ever 0

for (;ever;) {
/* this is never executed */
}

This would be extremely bad. Indeed. "never" would be a better name
(although the method is generally an abuse of the preprocessor ...)

#define LOOP_NEXT_BLOCK_FOREVER for(;;)

would be just as bad...

I usually do something like

/*infinit loop intentionally*/
for(;;)
{ /*short explaination of the purpose + why infinit*/

/*code goes here*/

}/*~for(;;)*/

all that, just to make things clear for later reading (in case my boss wants
to see what we are doing...)
(I even know people who just wrote for(;;) and intended to fill the missing
parts later on, but then again forgot to do so some lines later... I don't
know how they could get a programmers job, actually)
 
R

Richard Heathfield

John F said:
(I even know people who just wrote for(;;) and intended to fill the
missing parts later on, but then again forgot to do so some lines later...

For the record, my infinite loop program is still going strong (that's two
days now, I think), but the test so far has been inconclusive.
 
M

MrG{DRGN}

-snip
For the record, my infinite loop program is still going strong (that's two
days now, I think), but the test so far has been inconclusive.

Better hope the power doesn't go out!
 
R

Richard Heathfield

MrG{DRGN} said:
-snip

Better hope the power doesn't go out!

Well, that's what UPS is for. It's uninterruptible, right?

The food might run out. The oceans might dry up. The human race might die
out completely. The sun might bloat out into space and consume the earth in
a fiery death. The combined mass of the entire universe might reach its
expansion limit and then coalesce into the mother of all black holes.

But my UPS will still be supplying power to my PC, because that's what
"uninterruptible" means. And my PC must also somehow survive all this,
because it's running an *infinite* loop.
 
D

David Paleino

Richard Heathfield ha scritto:
...

Well, that's what UPS is for. It's uninterruptible, right?

The food might run out. The oceans might dry up. The human race might die
out completely. The sun might bloat out into space and consume the earth in
a fiery death. The combined mass of the entire universe might reach its
expansion limit and then coalesce into the mother of all black holes.

But my UPS will still be supplying power to my PC, because that's what
"uninterruptible" means. And my PC must also somehow survive all this,
because it's running an *infinite* loop.

*lol*!
So you solved one of the greatest misteries of the human kind: computers
will survive men, definitely. As there will always be at least one
computer on Earth that is running an infinite loop. Computers will get
their power from us. We will become computers' power suppliers.
Something like Matrix. :)

David
 
C

CBFalconer

Richard said:
John F said:


For the record, my infinite loop program is still going strong (that's two
days now, I think), but the test so far has been inconclusive.

Now if you can just arrange to extract a few ergs net from each
pass, we could be well on the way to energy independence.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
F

Fao, Sean

Richard said:
MrG{DRGN} said:


Well, that's what UPS is for. It's uninterruptible, right?

The food might run out. The oceans might dry up. The human race might die
out completely. The sun might bloat out into space and consume the earth in
a fiery death. The combined mass of the entire universe might reach its
expansion limit and then coalesce into the mother of all black holes.

But my UPS will still be supplying power to my PC, because that's what
"uninterruptible" means. And my PC must also somehow survive all this,
because it's running an *infinite* loop.

That was great! I needed that laugh...Thank you!
 
M

Man with Oscilloscope

rami said:


I have written a program for you, that contains an infinite loop, but it
wouldn't be right to show you the code until its test run is complete.

Richard,

I'd be very interested to see that code too, so... what's the status? Is
it anywhere near infinity yet?

Many thanks in advance.
 
R

Richard Heathfield

Man with Oscilloscope said:

I'd be very interested to see that code too, so... what's the status? Is
it anywhere near infinity yet?

137 days later... it's still going strong, but there's some way to go yet.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top