This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

gcc overoptimizes "volatile int"


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;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]