Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!virtech!cpcahil From: cpcahil@virtech.UUCP (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: evaluation order Keywords: which way Message-ID: <1149@virtech.UUCP> Date: 14 Sep 89 16:19:20 GMT References: <9361@attctc.Dallas.TX.US> Distribution: na Organization: Virtual Technologies Inc Lines: 42 In article <9361@attctc.Dallas.TX.US>, bobc@attctc.Dallas.TX.US (Bob Calbridge) writes: > Is there any way to guarantee the order in which certain functions are > evaluated? By way of example, if I wanted to avoid the replication of > of strlen() in the following example: > > if (write(handle, buf, strlen(buf)) != strlen(buf)) do_something(); > > by using rephrasing it like: > > if (write(handle, buf, len=strlen(buf)) != len) do_something(); > How about len = strlen(buf); if( write(...,len) != len)... > can I be assured that 'len' will be assigned the length of 'buf' before it > is used on the right side of the comparison operator. I understand that nope. > some optimizing compilers may do some odd re-arranging and I'm worried that > the value of 'len' before excuting this line of code may be used on the > right side. Is this really possible? Are there any assurances? There are no assurances as to the time the left or right side of the != are evaluated, nor to the time at which the assignment takes place (unless a sequence point has been reached). > I know that I can try this with my compiler and some test code but would it > be portable? Nope. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+