🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3657

gcc -O2 bug

#3657
From: Mike Big
Date: Wed, 07 Aug 1996 00:00
64 lines
2557 bytes
/* 
I'v found what I think might be a bug in gcc's optimization.  The
code below works corectly when compiled using "gcc -Wall -O1"
but fails when compiled using "gcc -Wall -O2".  I'v tried it using
both gcc 2.7.2 & 2.6.3 on Linux 1.3.100, Linux 2.0.10, & SunOS 4.1.3
w/ the same result.  The problem is that eventhough flag gets set to 
0, the while loop does NOT exit.  Also, when you uncomment the printf 
inside the loop, all is well again even w/ the -O2 optimizations.  
Any ideas are welcome.  (This is my first time using signals, so 
maybe something is wrong there but I still think the -O2 should not 
change things.)

Thanx in advance,
Mike
*/

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <signal.h>

void fca(); /* SIGALRM handler function */

long counter;
int *flag_pt;

int main()
{
  struct itimerval timer_length;
  int  flag;
  
  flag_pt=&flag;
  /* specify signal handler */
  signal(SIGALRM, fca);

  /* set the signal to repeat every 3 seconds */
  timer_length.it_value.tv_sec=3;     /* initial time */
  timer_length.it_value.tv_usec=0;
  timer_length.it_interval.tv_sec=3;  /* repeat */
  timer_length.it_interval.tv_usec=0;
  setitimer(ITIMER_REAL, &timer_length, NULL);
  
  flag=time(NULL); /* this is so that the compiler won't substitute a constant
                      for flag as it might if I did something like flag=1; */

  /* count until interrupted (after 3 seconds) */
  counter=0;
  while(flag){ /* does NOT exit when using gcc -O2 */
    counter++;
    /* uncomenting the printf below will fix things. weird heh */
    /* printf("counter=%ld flag=%d\n",counter,flag); */ 
  }

  exit(0);
}

void fca()
{
  printf("counter=%ld\n",counter);
  printf("before flag=%d\n",*flag_pt);
  *flag_pt=0;
  printf("after  flag=%d\n",*flag_pt);
}
Technical Details
Message-ID: <4ubnat$75u@freeside.fc.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,7d30cae02ba9b089,start
X-Google-Attributes: gid11318c,public
X-Google-Thread: 10c696,7d30cae02ba9b089,start
X-Google-Attributes: gid10c696,public
X-Google-Thread: fc89c,7d30cae02ba9b089,start
X-Google-Attributes: gidfc89c,public
X-Google-Thread: 1014db,7d30cae02ba9b089,start
X-Google-Attributes: gid1014db,public
From: mrbig@freeside.fc.net (Mike Big)
Subject: gcc -O2 bug
Date: 1996/08/07
Message-ID: <4ubnat$75u@freeside.fc.net>
X-Deja-AN: 172823472
organization: Freeside Communications, Inc.
keywords: gcc bug O2
newsgroups: comp.unix.programmer,gnu.gcc.help,comp.os.linux.development.apps,comp.lang.c