* C compiler problem
@ 1995-09-06 4:00 Basim Kadhim
1995-09-08 4:00 ` Steve Emmerson
1995-09-08 4:00 ` Basim Kadhim
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* Re: C compiler problem 1995-09-06 4:00 C compiler problem Basim Kadhim @ 1995-09-08 4:00 ` Steve Emmerson 1995-09-08 4:00 ` Basim Kadhim 1 sibling, 0 replies; 4+ messages in thread From: Steve Emmerson @ 1995-09-08 4:00 UTC (permalink / raw) 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: C compiler problem 1995-09-06 4:00 C compiler problem Basim Kadhim 1995-09-08 4:00 ` Steve Emmerson @ 1995-09-08 4:00 ` Basim Kadhim 1995-09-09 4:00 ` John Hascall 1 sibling, 1 reply; 4+ messages in thread From: Basim Kadhim @ 1995-09-08 4:00 UTC (permalink / raw) > 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: C compiler problem 1995-09-08 4:00 ` Basim Kadhim @ 1995-09-09 4:00 ` John Hascall 0 siblings, 0 replies; 4+ messages in thread From: John Hascall @ 1995-09-09 4:00 UTC (permalink / raw) Basim Kadhim <kadhim@cs.colorado.edu> wrote: > Steve Emmerson <steve@unidata.ucar.edu> wrote: } > kadhim@munge.cs.colorado.edu (Basim Kadhim) writes: } > > 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. }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. The comma operator is a sequence point, this means that in: expr1,expr2 that expr1 must be done completely before expr2 is. But, the comma between function arguments is different, it is not an operator, but "syntactic sugar" if you will. so in: function( expr3, expr4 ) we have no assurance in what order expr3 and expr4 may be evaluated, including the possibility of interleaving. so, in: function( (exprA,exprB) , (exprC,exprD) ) we can say only that A happens before B and C happens before D, so all of these orders are equally legal: ABCD , ACBD , ACDB , CABD, CADB and we haven't even started to think about dividing A B C and D up yet. ------- insert standard disclaimer about "as if" to suit. John -- John Hascall ``An ill-chosen word is the fool's messenger.'' Moderator, comp.unix.wizards Systems Software Engineer, ISU Comp Center + Ames, IA 50011 + 515/294-9551 <a href="http://www.cc.iastate.edu/staff/systems/john/">My Homepage</a> y;x(c){isalnum(y=c=getchar())&&x()|putchar(c);}main(){for(;x(),~y;putchar(y));} ^ 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 ` Steve Emmerson 1995-09-08 4:00 ` Basim Kadhim 1995-09-09 4:00 ` John Hascall
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox