This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
gcc overoptimizes "volatile int"
- To: gcc-bugs at gcc dot gnu dot org
- Subject: gcc overoptimizes "volatile int"
- From: "Thomas R. Truscott" <trt at cs dot duke dot edu>
- Date: Wed, 2 Aug 2000 16:12:16 -0400 (EDT)
In ss-20000724 i686-pc-linux (and since sometime this year)
a "gcc -O" keeps "volatile int" in a register,
which causes the program below to loop infinitely.
(Sorry I couldn't come up with something simpler.)
The program sets a volatile int "d" to be non-zero,
takes its address, and loops while d is non-zero.
A timer goes off and changes the value of d,
but (with -O) the loop keeps going.
Tom Truscott
=================================
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
volatile int *dp;
void
dosig (int n)
{
printf ("dosig (%d)\n", n);
*dp = 0;
}
int
main ()
{
volatile int d;
signal (SIGALRM, dosig);
alarm(2);
d = 1;
dp = &d;
printf ("loop begins\n");
while (d)
;
printf ("loop ends\n");
return 0;
}