🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4416

Re: On sequence points and evaluation order

#4416
From: Eivind Eklund
Date: Sat, 02 Dec 1995 17:09
51 lines
2712 bytes
In article <49l428$od0@bolverk.cs.cornell.edu>,
	nickie@cs.cornell.edu (Nikolaos Papaspyrou) writes:
>
>
>I have a couple of questions regarding side effects, order of
>evaluation and the meaning of a "sequence point" in the standard.
>I would appreciate if somebody could take some time to answer...
>
>
>Question 1: Consider the following two programs.
>
>   int i = 0;                     int i = 0;
>
>   int main () {                  int f () { return (i = 1); }
>      return (i = 1) + i;
>   }                              int main () {
>                                     return f() + i;
>                                  }
>
>As far as common sense is concerned, the two programs are equivalent.
>I believe the behavior of the program on the left is undefined
>according to the standard, since between two consecutive sequence
>points (before and after the return statement) a write and a read
>operation occur for object "i", and the read operation does not intend
>to calculate the value that the write operation will store.
>
>The program on the right does exactly the same thing, only there seems
>to be an extra sequence point just before function "f" is called. Now
>the read and write operations do not occur between two successive
>sequence points. Therefore (please correct me in this, for I believe
>this statement is wrong), the behavior of this program is not
>undefined (so what is it?).

The sequence point is not the same as order of evaluation determination.  
The sequence point for the function call is regarding the expressions 
inside the function parameter list; they will be evaluated before the 
function is called.  This do, however, not determine whether the rest of 
the expression the function call appear in have been evaluated.  (The 
sequence point also mean that the values of all variables have to be in 
memory at this point; ie, the function can read any variable it have 
some way of getting access to, and get a value that correspond to what 
the rest of the program believe it is.)

The value of i in the expression return f() + i; can be evaluated before 
or after the function call, so it is invoking undefined behaviour (order 
of evaluation.)

-- 
Eivind Eklund
eivind.eklund@funcom.com
FUNCOM don't speak for me, and I return the courtesy.
Technical Details
Message-ID: <49q17v$k9j@odin.funcom.no>
Path: arc1-ca.pugleaf.net!funcom.no!news
References: <49l428$od0@bolverk.cs.cornell.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!funcom.no!news
From: eivind@odin.funcom.com (Eivind Eklund)
Newsgroups: comp.std.c
Subject: Re: On sequence points and evaluation order
Date: 2 Dec 1995 17:09:19 GMT
Organization: Funcom Productions.
Message-ID: <49q17v$k9j@odin.funcom.no>
References: <49l428$od0@bolverk.cs.cornell.edu>
NNTP-Posting-Host: odin.funcom.no
X-Newsreader: knews 0.9.3