Article View: comp.unix.programmer
Article #3665Re: gcc -O2 bug
From: Preston F. Crow
Date: Thu, 08 Aug 1996 00:00
Date: Thu, 08 Aug 1996 00:00
85 lines
3767 bytes
3767 bytes
As someone else pointed out already, the problem is that gcc will
optimize the loop, recognizing that there's nothing in the loop that
can modify 'flag,' so the test is performed only once (if even then).
The most surprising thing to me is that adding printf() results in the
loop working. This ultimately makes sense, though, as the compiler
doesn't know for certain that printf() doesn't have a pointer to flag,
so it can't assume that it won't be modified. As printf() takes
pointer arguments, the prototype can't use the gcc extensions to tell
the compiler that it won't have side effects. I suspect that most
versions of the include files don't have those extensions, anyway,
though I would hope that especially the math functions would.
--PC
For reference:
mrbig@freeside.fc.net (Mike Big) writes:
>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.)
>#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);
>}
--
The position and/or opinions in the above message are those of the Board of
Trustees of Dartmouth College. Copyright (c) 1996. All right reserved. The
above positions are also held by Newt Gingrich, many foriegn world leaders, and
you who are reading them. Preston bears no responsibility for the content.
Technical Details
Message-ID:
<4udo9v$in0@dartvax.dartmouth.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<4ubnat$75u@freeside.fc.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,7d30cae02ba9b089 X-Google-Attributes: gid11318c,public X-Google-Thread: 10c696,7d30cae02ba9b089 X-Google-Attributes: gid10c696,public X-Google-Thread: fc89c,7d30cae02ba9b089 X-Google-Attributes: gidfc89c,public X-Google-Thread: 1014db,7d30cae02ba9b089 X-Google-Attributes: gid1014db,public From: crow@coos.dartmouth.edu (Preston F. Crow) Subject: Re: gcc -O2 bug Date: 1996/08/08 Message-ID: <4udo9v$in0@dartvax.dartmouth.edu> X-Deja-AN: 172997866 references: <4ubnat$75u@freeside.fc.net> organization: Dartmouth College, Hanover, NH, USA keywords: gcc bug O2 newsgroups: comp.unix.programmer,gnu.gcc.help,comp.os.linux.development.apps,comp.lang.c