Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: kadhim@cs.colorado.edu (Basim Kadhim)
Subject: Re: C compiler problem
Date: 1995/09/08	[thread overview]
Message-ID: <KADHIM.95Sep8130540@spock.cs.colorado.edu> (raw)
In-Reply-To: KADHIM.95Sep6103902@munge.cs.colorado.edu

 > kadhim@munge.cs.colorado.edu (Basim Kadhim) writes:
 > 
 > >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)));
 > >}
 > 
 > Because the above program doesn't conform to the C Standard, it is
 > inappropriate to say the the compiler is behaving `strangely'.  It is
 > more correct to say that the program is relying upon undefined behavior.
 > 
 > The program doesn't conform to the C Standard because the value of `x'
 > is being modified more than once between adjacent sequence points.
 > This violates the `shall' constraint imposed by Section 3.3 of the
 > Standard.  The argument-separating comma is not the comma operator
 > (Section 3.3.17); thus, there is no sequence point separating the
 > evaluation of the two arguments to the add() function; consequently, the
 > compiler may pretty much do what it wishes to optimize evaluation.
 > 
 > Moral: if you don't want `strange' behavior, then write conforming code.
 > --
 > 
 > Steve Emmerson        steve@unidata.ucar.edu        ...!ncar!unidata!steve

While what you say may have been the intent of those writing the standard,
I believe the standard is quite unclear on whether the above program
conforms to the standard or not.  I saw the section you're talking about
and from what I saw, the standard is not at all clear about what "adjacency"
of sequence points means.  Since the comma operator present in each of
the argument expressions IS a sequence point, by any notion of adjacency
that I can think of there isn't more than one modification to 'x' between
adjacent sequence points.

While I agree that there doesn't seem to be anything in the standard that
would strictly prohibit the compiler from interleaving evaluation as it
does, the standard does prohibit things like exploiting the associativity
and commutativity of operators if doing so might yield different results
(because of rounding and possible exceptions).  This kind of statement
makes me side with Harbison & Steele on the issue of suggesting that
compilers should behave such that arguments are evaluated completely
before evaluating another.
--
Basim Kadhim					kadhim@cs.colorado.edu
University of Colorado, Boulder



  parent reply	other threads:[~1995-09-08  4:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-09-06  4:00 C compiler problem Basim Kadhim
1995-09-08  4:00 ` Steve Emmerson
1995-09-08  4:00 ` Basim Kadhim [this message]
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