🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4441

Re: On sequence points and evaluation order

#4441
From: Lawrence Kirby
Date: Thu, 07 Dec 1995 13:40
72 lines
3368 bytes
In article <49vgb5$muj@lyra.csx.cam.ac.uk>
           nmm1@cus.cam.ac.uk "Nick Maclaren" writes:

>In article <49qtc0$72g@light.twinsun.com>,
>Paul Eggert <eggert@twinsun.com> wrote:
>>This point has been debated before.  Some people think the standard
>>says `f()+f()' has undefined behavior when f has side effects
>>(e.g. if f is defined by `int f() { static int i; return i++; }').
>>Other people think the standard defines behavior for `f()+f()'.
>>Still other people think that that this example has undefined behavior,
>>but other, similar examples have defined behavior.
>
>The example that really baffles me is '(++i,j)+(j,++i)', because the

                                         -a- b   c -d-


>standard assumes an ordering of operations without defining what that
>ordering is.  If anyone thinks that this example is easy, write out
>the parse tree and consider its consequences!

This expression defines a partial ordering. Using the subexpression labels I
have shown expression, there is a sequence point between expressions a and
b and also between c and d. There is no sequence point between a and b
and c and d since + does not define any sequence point. In particular the
following order of evaluation is permitted with . indicating a sequence point:

c . d a . b

Therefore i is modified twice between sequence points and the expression
has undefined behaviour. Looking at a simple parse tree:

                          .
                         .  +
                        .  / \
                       .  / . \
                      .  /   . \
                     .  /     . \
                    .  /       . \
                   .  ,         . ,
                  .  / \       . / \
                 .  /   \     . / . \
                   a     b   . c   . d

The dots indicate the 'scope' of a side effect in expression a. You'll see
that the sequence point in the comma only protects expression b from it.
Then you just have to work out how to deal with something like:

(j,++i,j) + (j,++i,j)

The rule seems to be "if you can find any permissible ordering (subject
to the partial orderings defined by sequence points) which would result in
undefined behaviour according to the standard then the expression has
undefined behaviour". It may be possible to derive that in the
general case from the parse tree (at least where the objects that side
effects act on can be identified at compile time) but the rules are probably
not trivial.

I would say in your expression the following orderings are possible:

a . b c . d
c . d a . b
a c . b d
a c . d b
c a . b d
c a . d b

-- 
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Technical Details
Message-ID: <818343602snz@genesis.demon.co.uk>
Path: arc1-ca.pugleaf.net!mail2news.demon.co.uk!genesis.demon.co.uk
References: <49l428$od0@bolverk.cs.cornell.edu> <49qtc0$72g@light.twinsun.com> <49vgb5$muj@lyra.csx.cam.ac.uk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!mail2news.demon.co.uk!genesis.demon.co.uk
From: Lawrence Kirby <fred@genesis.demon.co.uk>
Newsgroups: comp.std.c
Subject: Re: On sequence points and evaluation order
Date: Thu, 07 Dec 95 13:40:02 GMT
Organization: none
Message-ID: <818343602snz@genesis.demon.co.uk>
References: <49l428$od0@bolverk.cs.cornell.edu> <49qtc0$72g@light.twinsun.com> <49vgb5$muj@lyra.csx.cam.ac.uk>
Reply-To: fred@genesis.demon.co.uk
X-NNTP-Posting-Host: genesis.demon.co.uk
X-Newsreader: Demon Internet Simple News v1.27