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: john@iastate.edu (John Hascall) Subject: Re: C compiler problem Date: 1995/09/09 Message-ID: <42smt9$9v8@news.iastate.edu>#1/1 X-Deja-AN: 109793493 references: organization: Iowa State University, Ames, Iowa USA newsgroups: comp.unix.osf.osf1 Basim Kadhim wrote: > Steve Emmerson 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 My Homepage y;x(c){isalnum(y=c=getchar())&&x()|putchar(c);}main(){for(;x(),~y;putchar(y));}