John Regehr
2010-05-03 16:06:34 UTC
The C99 standard, as I read it, is not completely clear about whether
a conforming implementation may change the termination behavior of a
conforming program. In contrast, the Java language definition makes
it clear that the JVM must not terminate an infinite loop. The C++0x
draft standard is explicit that an implementation *can* terminate an
infinite loop.
The arguments seem to be as follows:
One one hand: Non-terminating executions are not mentioned in the list
of "least requirements" in 5.1.2.3 of N1124, so the compiler is free
to terminate an infinite loop.
On the other hand: Termination of infinite loops will change the side
effecting operations performed by a program, so a conforming
implementation must not do this.
Can anyone comment on the intent of the standards committee on this
point? Is there any chance this can be explicitly cleared up in a
future standard?
Consider this example, where "icc" is the current version of Intel's
compiler for x86:
***@john-home:~$ icc fermat2.c -o fermat2
***@john-home:~$ ./fermat2
Fermat's Last Theorem has been disproved.
Where fermat2.c is this code:
int fermat (void)
{
const int MAX = 1000;
int a=1,b=1,c=1;
while (1) {
if (((a*a*a) == ((b*b*b)+(c*c*c)))) return 1;
a++;
if (a>MAX) {
a=1;
b++;
}
if (b>MAX) {
b=1;
c++;
}
if (c>MAX) {
c=1;
}
}
return 0;
}
#include <stdio.h>
int main (void)
{
if (fermat()) {
printf ("Fermat's Last Theorem has been disproved.\n");
} else {
printf ("Fermat's Last Theorem has not been disproved.\n");
}
return 0;
}
A number of other C implementations have this behavior, whereas others
preserve the termination behavior of their inputs.
The other day I wrote a slightly lighthearted blog entry about this
issue and it generated quite a bit of discussion:
http://blog.regehr.org/archives/140
Thanks.
John Regehr
a conforming implementation may change the termination behavior of a
conforming program. In contrast, the Java language definition makes
it clear that the JVM must not terminate an infinite loop. The C++0x
draft standard is explicit that an implementation *can* terminate an
infinite loop.
The arguments seem to be as follows:
One one hand: Non-terminating executions are not mentioned in the list
of "least requirements" in 5.1.2.3 of N1124, so the compiler is free
to terminate an infinite loop.
On the other hand: Termination of infinite loops will change the side
effecting operations performed by a program, so a conforming
implementation must not do this.
Can anyone comment on the intent of the standards committee on this
point? Is there any chance this can be explicitly cleared up in a
future standard?
Consider this example, where "icc" is the current version of Intel's
compiler for x86:
***@john-home:~$ icc fermat2.c -o fermat2
***@john-home:~$ ./fermat2
Fermat's Last Theorem has been disproved.
Where fermat2.c is this code:
int fermat (void)
{
const int MAX = 1000;
int a=1,b=1,c=1;
while (1) {
if (((a*a*a) == ((b*b*b)+(c*c*c)))) return 1;
a++;
if (a>MAX) {
a=1;
b++;
}
if (b>MAX) {
b=1;
c++;
}
if (c>MAX) {
c=1;
}
}
return 0;
}
#include <stdio.h>
int main (void)
{
if (fermat()) {
printf ("Fermat's Last Theorem has been disproved.\n");
} else {
printf ("Fermat's Last Theorem has not been disproved.\n");
}
return 0;
}
A number of other C implementations have this behavior, whereas others
preserve the termination behavior of their inputs.
The other day I wrote a slightly lighthearted blog entry about this
issue and it generated quite a bit of discussion:
http://blog.regehr.org/archives/140
Thanks.
John Regehr