From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,512b17e0104b2ceb X-Google-Attributes: gidfee84,public From: kadhim@cs.colorado.edu (Basim Kadhim) Subject: Re: C compiler problem Date: 1995/09/08 Message-ID: #1/1 X-Deja-AN: 109793488 distribution: world references: organization: Dept. of Computer Science, University of Colorado, Boulder reply-to: kadhim@cs.colorado.edu newsgroups: comp.unix.osf.osf1 > kadhim@munge.cs.colorado.edu (Basim Kadhim) writes: > > >The Alpha C compiler behaves quite strangely with comma expressions. > >Consider the following example: > > >#include > > >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