 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Feb 27, 2007 3:03 am Post subject: what's consensus or standard on what should "while(1){}" do? |
|
|
hi
what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Hans-Bernhard Brφker Guest
|
Posted: Thu Mar 01, 2007 8:33 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
mainargv (AT) yahoo (DOT) com wrote:
| Quote: | what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing?
|
The Standard clearly says that it must loop forever. I really wonder
what makes you suspect otherwise.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Thu Mar 01, 2007 8:34 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
mainargv (AT) yahoo (DOT) com wrote:
| Quote: | what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
|
According to the C standard, it must perform the equivalent
function of a forever loop. Why would you think otherwise?
Note that that doesn't mean that no useful work can be done
by the program, for example if signal handlers were first
set up and get invoked occasionally by asynchronous signals.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Thu Mar 01, 2007 8:34 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
On 26 Feb 2007 21:03:12 GMT, mainargv (AT) yahoo (DOT) com wrote in
comp.lang.c.moderated:
| Quote: | hi
what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
|
What makes you think consensus has anything to do with it? What does
your C reference book, online help, or man pages say?
The C standard defines "while ( expression ) statement" as an
iteration statement, and it says this about iteration statements:
"An iteration statement causes a statement called the loop body to be
executed repeatedly until the controlling expression compares equal to
0."
So what's your consensus about the expression (1) ever comparing equal
to 0?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
WillerZ Guest
|
Posted: Thu Mar 01, 2007 8:34 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
mainargv (AT) yahoo (DOT) com wrote:
| Quote: | hi
what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
|
It must loop forever. Any compiler which caused it to do nothing would
be incorrect.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
David Given Guest
|
Posted: Thu Mar 01, 2007 8:35 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
mainargv (AT) yahoo (DOT) com wrote:
[...]
| Quote: | what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
|
Well, both.
Read the statement as: while 1 is non-zero, do nothing.
Since 1 is *always* non-zero, it will repeatedly do nothing forever.
--
βββ ο½ο½οΌ ο½ο½ο½ο½ο½ο½ο½οΌο½ο½ο½ βββ http://www.cowlark.com βββββββββββββββββββ
β
β "The first 90% of the code takes the first 90% of the time. The other 10%
β takes the other 90% of the time." --- Anonymous
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Kenneth Brody Guest
|
Posted: Thu Mar 01, 2007 8:36 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
mainargv (AT) yahoo (DOT) com wrote:
| Quote: |
hi
what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
|
The "consensus" isn't really relevent.
It will do exactly what you have told it to do -- loop forever doing
nothing. What makes you think that another option is available?
On my particular implementation, the compiler generates this code:
$L523:
jmp $L523
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap (AT) gmail (DOT) com>
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
R Pradeep Chandran Guest
|
Posted: Thu Mar 01, 2007 8:36 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
On 26 Feb 2007 21:03:12 GMT, mainargv (AT) yahoo (DOT) com wrote:
| Quote: | what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
|
As far as I can see, the standard requires "while(1){}" to loop
forever.
According to section 6.8.2 of ISO/IEC 9899:1999, "{}" is a valid
compound statement. Sections 6.8.5 requires the loop body to be
executed until the controlling expression compares equal to zero which
in the case of "while(1){}" will not happen.
As far as I can see, "while(1);" and "while(1){}" are practically
equivalent.
Have a nice day,
Pradeep
--
All opinions are mine and do not represent the views or
policies of my employer.
R Pradeep Chandran rpc AT pobox DOT com
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Mar 06, 2007 2:42 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
On Mar 1, 10:36 am, R Pradeep Chandran <s...@sig.below> wrote:
| Quote: | On 26 Feb 2007 21:03:12 GMT, maina...@yahoo.com wrote:
what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
As far as I can see, the standard requires "while(1){}" to loop
forever.
According to section 6.8.2 of ISO/IEC 9899:1999, "{}" is a valid
compound statement. Sections 6.8.5 requires the loop body to be
executed until the controlling expression compares equal to zero which
in the case of "while(1){}" will not happen.
As far as I can see, "while(1);" and "while(1){}" are practically
equivalent.
Have a nice day,
Pradeep
--
All opinions are mine and do not represent the views or
policies of my employer.
R Pradeep Chandran rpc AT pobox DOT com
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
ok, so any compiler that optimize that statement away is wrong
(against ISO standard). thanks
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Mar 06, 2007 2:42 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
On Mar 1, 10:36 am, Kenneth Brody <kenbr...@spamcop.net> wrote:
| Quote: | maina...@yahoo.com wrote:
hi
what's consensus or standard on what should "while(1){}" do? loop
forever or do nothing? thanks
The "consensus" isn't really relevent.
It will do exactly what you have told it to do -- loop forever doing
nothing. What makes you think that another option is available?
On my particular implementation, the compiler generates this code:
$L523:
jmp $L523
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |www.hvcomputer.com| #include |
| kenbrody/at\spamcop.net |www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamT...@gmail.com
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
|
my particular compiler will do differently depends on something
else...
I'm just not too sure which way to interpret it. doing nothing forever
can in some sense as not doing it in the first place (humans will
reach this sort of conclusion anyway.)
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Douglas A. Gwyn Guest
|
Posted: Sat Mar 10, 2007 6:48 pm Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
mainargv (AT) yahoo (DOT) com wrote:
| Quote: | my particular compiler will do differently depends on something
else...
|
It shouldn't. Maybe your compiler is broken, or maybe you have
a bug in "something elee" that results in confusing behavior.
| Quote: | I'm just not too sure which way to interpret it. doing nothing forever
can in some sense as not doing it in the first place (humans will
reach this sort of conclusion anyway.)
|
There is a big difference. Not doing it at all takes 0 time
and immediately proceeds to do the next thing in sequence.
Doing nothing forever takes infinite time and never gets to
the next thing in sequence.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Edward Rosten Guest
|
Posted: Thu Mar 29, 2007 6:20 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
On Mar 10, 6:48 am, "Douglas A. Gwyn" <DAG...@null.net> wrote:
| Quote: | Doing nothing forever takes infinite time and never gets to
the next thing in sequence.
|
Without meaning to be too pedantic, it's doing nothing an infinite
number of times. That's different, since infinity*0 can equal
anything.
consider
{ int i; for(i=0; i < INT_MAX; i++); }
The compiler can optimize that away. It has no side effects, apart
from taking time, so it can be eliminated.
for(; ;
is the same, it has no side effects apart from time. Eliminating it
simply makes the loop body infinity^2 times faster.
-Ed
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Edward Rosten Guest
|
Posted: Thu Mar 29, 2007 6:30 am Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
On Mar 10, 6:48 am, "Douglas A. Gwyn" <DAG...@null.net> wrote:
| Quote: | I'm just not too sure which way to interpret it. doing nothing forever
can in some sense as not doing it in the first place (humans will
reach this sort of conclusion anyway.)
There is a big difference. Not doing it at all takes 0 time
and immediately proceeds to do the next thing in sequence.
Doing nothing forever takes infinite time and never gets to
the next thing in sequence.
|
That's just a big win for the optimizer. But seriously, can the
compiler optimize away this E()?
#define A(X) { unsigned long a; for(a=0;a < 0xffffffff; a++){X}}
#define B(X)
A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(X))))))))))))))))))))
#define C(X)
B(B(B(B(B(B(B(B(B(B(B(B(B(B(B(B(B(B(B(B(X))))))))))))))))))))
#define D(X)
C(C(C(C(C(C(C(C(C(C(C(C(C(C(C(C(C(C(C(C(X))))))))))))))))))))
#define E(X)
D(D(D(D(D(D(D(D(D(D(D(D(D(D(D(D(D(D(D(D(X))))))))))))))))))))
int main()
{
//some stuff
E();
//more stuff
}
The block has no side effects, so the compiler could remove it without
altering the output of the program. In fact gcc does indeed remove the
offending code (well, up to C(), above that it runs out of memory). In
case you're not interested in counting it by hand, there are 160000
nested for loops with 4 billion iterations in each, in other words the
compiler can optimize away 4294967296^160000 operations. Clearly,
saving arbitrarily large amounts of time is allowed. Why not infinite
amounts? There is not enough energy present in the universe to
complete the above computation without the optimizations, so
eliminating E(), and eliminating an infinite loop produces the same
results ( more stuff will be executed, as opposed to not executed).
-Ed
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Thu Apr 19, 2007 7:13 pm Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> writes:
| Quote: | In message <clcm-20070328-0004 (AT) plethora (DOT) net>, Edward Rosten
Edward.Rosten (AT) gmail (DOT) com> writes
for(; ;
is the same, it has no side effects apart from time. Eliminating it
simply makes the loop body infinity^2 times faster.
Interesting whether a compiler is allowed to optimise away that loop.
Granted it does nothing yet optimising it away certainly changes the
observable behaviour:
#include <stdio.h
int main(){
for(; ;
puts("I am here.\n");
return 0;
}
Without the optimisation you will never see any output, with it you
will.
Comments?
|
Just as a matter of common sense, an optimizer that eliminates the
"for(; ;" statement is broken. An optimizer *could* eliminate the
puts() call, since it will never be executed.
If there were a rigorous formal definition of the execution of C
statements, presumably it could be used to prove that the puts() is
never executed.
Consider an equivalent snippet:
while (1+1==2);
puts("I am here.\n");
After a while loop terminates, we know (barring side effects) that the
condition must be false. If this program prints "I am here.", we can
assume that 1+1 is no longer equal to 2. In a "for(; " loop, the
condition is implicitly 1; if your program prints "I am here.", we can
assume that 1 is no longer true.
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
Mark L Pappin Guest
|
Posted: Sat Apr 21, 2007 5:16 pm Post subject: Re: what's consensus or standard on what should "while(1){}" |
|
|
Keith Thompson <kst-u (AT) mib (DOT) org> writes:
| Quote: | Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> writes:
Granted it does nothing yet optimising [an infinite loop of
nothing] away certainly changes the observable behaviour:
#include <stdio.h
int main(){
for(; ;
puts("I am here.\n");
return 0;
}
An optimizer *could* eliminate the puts() call, since it will never
be executed.
|
Our optimizer does. I have had a customer complain that the
single-character typo in
while(1);
{
/* do stuff forever,
a common embedded practice */
}
was accepted by the compiler, and moreover generated no code for the
block. I pointed out the warning about unreachable code that had been
emitted, but to no avail.
mlp
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|