Must useless expressions be evaluated?

T.J.Teixeira tjt at kobold.UUCP
Tue Oct 18 00:59:29 AEST 1983


As others have pointed out, dereferencing a pointer might generate an
illegal memory reference.  Since this could generate a signal which
could be caught by the program, the side effect of the dereferencing
should be visible to the program.

One might even go so far as to say that using computation time is a
side effect.  If the program has access to a cpu-time clock (i.e.
times(II) in UNIX) (or less reliably, just a real-time clock) you could
write a program for a particular machine that depends on the side
effect of the execution time.

Although very few programs rely on these side effects, benchmark
programs are an important example.  How many benchmarks have you seen
that compute some numbers and then just throw them away?  If you don't
even print them out, a compiler could just decide to optimize your
program to nothing!

I believe that something like this actually did happen several years
ago in somebodys us vs. them ads: the ratio between the execution times
was something like 100000.  I believe that the particular example was
supposed to be a FORTRAN program with lots and lots of GOTO's.  The
intention was to determine how fast the machine could execute a branch
instruction.  However, one of the compilers just unravelled the
branches and threw them all away, leading to the astronomically better
apparent performance.



More information about the Comp.lang.c mailing list