From: kadhim@munge.cs.colorado.edu (Basim Kadhim)
Subject: C compiler problem
Date: 1995/09/06 [thread overview]
Message-ID: <KADHIM.95Sep6103902@munge.cs.colorado.edu> (raw)
The Alpha C compiler behaves quite strangely with comma expressions.
Consider the following example:
#include <stdio.h>
int
add (a, b)
{
return a + b;
}
int
main ()
{
int x;
printf("%d\n", add((x=5,x),(x=4,x)));
}
Compiling the program with cc and running yields the value 8, which quite
obviously indicates that the compiler is somehow interleaving the computation
of the function arguments. When compiled with the "-migrate" flag, the
program yields 9 as one would expect.
I went looking through the ANSI C standard and didn't find anything that
expressly disallows this interleaving behavior, which suggests that strictly
speaking it doesn't violate the standard, however the Harbison & Steele
book on C has the following to say:
The original description of C specified that subexpressions may be
evaluated in any order and that the arguments to a function may be
evaluated in any order. The matter of interleaving was not discussed,
nor the question of whether rearranging may alter the implicit type
conversions. We advise implementors to adhere rigidly to the
restrictions outlined here (which actually are quite sensible and
not terribly restrictive).
A little earlier they state:
When evaluating the actual arguments in a function call, the order in
which the arguments are evaluated is not specified; but the program must
behave as if it chose one argument, evaluated it fully, then chose another
argument, evaluated it fully, and so on until all arguments were
evaluated.
It sure would be nice if the Alpha compiler adhered to this.
--
Basim Kadhim kadhim@cs.colorado.edu
University of Colorado, Boulder
next reply other threads:[~1995-09-06 4:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1995-09-06 4:00 Basim Kadhim [this message]
1995-09-08 4:00 ` C compiler problem Steve Emmerson
1995-09-08 4:00 ` Basim Kadhim
1995-09-09 4:00 ` John Hascall
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox