Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
* C compiler problem
@ 1995-09-06  4:00 Basim Kadhim
  1995-09-08  4:00 ` Basim Kadhim
  1995-09-08  4:00 ` Steve Emmerson
  0 siblings, 2 replies; 4+ messages in thread
From: Basim Kadhim @ 1995-09-06  4:00 UTC (permalink / 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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1995-09-09  4:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1995-09-06  4:00 C compiler problem Basim Kadhim
1995-09-08  4:00 ` Basim Kadhim
1995-09-09  4:00   ` John Hascall
1995-09-08  4:00 ` Steve Emmerson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox