Topic: A couple sequence point riddles -


Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/11/30
Raw View
On Fri, 26 Nov 1999 21:39:19 CST, fvali <fvali@kumc.edu> wrote:

: I know this subject has been beaten to death - but I just had a few
: quick questions -

I'll give it a shot and add a couple.

: SP = sequence point.
:
: int f( int& ) { return 6; }
: int g( int& ) { return 7; }
:
:
: Is this well defined?
: i = f( ++i );
: SP after evaluation of ++i and after return of f.
: But there is no SP per se between assignment of i and increment of i,
: therefore is this undefined?

No.  The increment and its side effects must be completed prior to
the call of f.  Both SP are between ++ i and i =.

: Anyone know if I am right on these:
:
: This is clearly undefined -
: int j = f( ++i ) + g( ++i );

Yes.  Almost identical to a GotW in clc++m.

: This is well defined -
: int k = ++i ? ++i : 0;

Yes.  SE after first ++i.

: BUT What about this?
: int k = ++i ? ++i : ++i;

No difference.  Se after first ++i and only one of the others is
executed.

: And this is clearly undefined right?
: int k = ++i ? ++i : ++i + ++i ? ++i : ++i;

Yes.

  int k = ++i ? ++i : (++i + ++i ? ++i : ++i);

No SE between the two ++i in the condition of the second ?

Were you thinking of

  int k = (++i ? ++i : ++i) + (++i ? ++i : ++i);

Still undefined.  Sequence point after each condition, but none
between them.  They could be evaluated in parallel.


Those were all the did I modify something twice without a sequence
point questions.  The harder ones are when there is only one
modification.

f(i, ++i);
f(++i, i);
Undefined because there is no sequence point between the use of i
and the increment.

i = x[i];
Well defined because i was used on the right to determine the new
value of i.

Assume a doubly linked list with succ and pred pointers.

p = p->succ;
Well defined as above.

p = p->succ = p->succ->succ;
Was p on the far right used to determine the new value of p?  I
think so.  Was the p in the middle used to determine the new value
of p?  Not sure, but it seems well defined.

(p->pred->succ = p->succ)->pred = p->pred;
Well defined because the value of the first assignment is used in
the calculation of the lhs of the second assignment.  No use of
the old value of anything which is modified.

p->pred = p->pred->succ = new Node(p, p->pred);
Well defined for the parameter, p->pred, to Node().  How about the
center p->pred?  Seems all right to me.

q = new Node;
(((q->pred = p->pred)->succ = q)->succ = p)->pred = q;
Is the first use of p->pred used to determine the new value of itself?
Seems like it is.

I expect them to all produce the desired results, but am not sure
about the standard statement.

John
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: kanze@gabi-soft.de
Date: 1999/11/27
Raw View
fvali <fvali@kumc.edu> writes:

|>  I know this subject has been beaten to death - but I just had a few
|>  quick questions -
|>
|>  SP = sequence point.
|>
|>  int f( int& ) { return 6; }
|>  int g( int& ) { return 7; }
|>
|>  Is this well defined?
|>  i = f( ++i );
|>  SP after evaluation of ++i and after return of f.
|>  But there is no SP per se between assignment of i and increment of i,
|>  therefore is this undefined?

Well defined.  Evaluation of the expression to be assigned to i must
precede the assignment.  Although the assignment itself doesn't impose a
sequence point, evaluating the expression does require that f be called,
so the sequence point occurs.

|>  Anyone know if I am right on these:

|>  This is clearly undefined -
|>  int j = f( ++i ) + g( ++i );

|>  This is well defined -
|>  int k = ++i ? ++i : 0;

|>  BUT What about this?
|>  int k = ++i ? ++i : ++i;

Well defined.  The undefined behavior comes not from the lexical form of
the expression, but from trying to modify the value without an
intervening sequence point.  There *is* a sequence point at the ?,
after evaluation of the first ++i, and following that, only one side of
the : can be executed.

|>  And this is clearly undefined right?
|>  int k = ++i ? ++i : ++i + ++i ? ++i : ++i;

I think that the precedence gives:

    int k = ++i ? ++i : ((++i + ++i) ? ++i : ++i) ;

If so, The ++i + ++i is obviously undefined behavior.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/11/27
Raw View
In article <864se93ya0.fsf@gabi-soft.de>, kanze@gabi-soft.de writes
>|>  int f( int& ) { return 6; }
>|>  int g( int& ) { return 7; }
>|>
>|>  Is this well defined?
>|>  i = f( ++i );
>|>  SP after evaluation of ++i and after return of f.
>|>  But there is no SP per se between assignment of i and increment of i,
>|>  therefore is this undefined?
>
>Well defined.  Evaluation of the expression to be assigned to i must
>precede the assignment.  Although the assignment itself doesn't impose a
>sequence point, evaluating the expression does require that f be called,
>so the sequence point occurs.

However compilers such as VC++ that will implicitly inline the call to f
had better get the resulting code correct.


Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: kanze@gabi-soft.de
Date: 1999/11/28
Raw View
Francis Glassborow <francis@robinton.demon.co.uk> writes:

|>  In article <864se93ya0.fsf@gabi-soft.de>, kanze@gabi-soft.de writes
|>  >|>  int f( int& ) { return 6; }
|>  >|>  int g( int& ) { return 7; }

|>  >|>  Is this well defined?
|>  >|>  i = f( ++i );
|>  >|>  SP after evaluation of ++i and after return of f.
|>  >|>  But there is no SP per se between assignment of i and increment of i,
|>  >|>  therefore is this undefined?

|>  >Well defined.  Evaluation of the expression to be assigned to i must
|>  >precede the assignment.  Although the assignment itself doesn't impose a
|>  >sequence point, evaluating the expression does require that f be called,
|>  >so the sequence point occurs.

|>  However compilers such as VC++ that will implicitly inline the call to f
|>  had better get the resulting code correct.

But of course.  (And VC++ is not alone in this. G++ -- and I imagine
most other good compilers -- will also inline in such cases.)

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: fvali <fvali@kumc.edu>
Date: 1999/11/26
Raw View
I know this subject has been beaten to death - but I just had a few
quick questions -

SP = sequence point.

int f( int& ) { return 6; }
int g( int& ) { return 7; }


Is this well defined?
i = f( ++i );
SP after evaluation of ++i and after return of f.
But there is no SP per se between assignment of i and increment of i,
therefore is this undefined?

Anyone know if I am right on these:

This is clearly undefined -
int j = f( ++i ) + g( ++i );

This is well defined -
int k = ++i ? ++i : 0;

BUT What about this?
int k = ++i ? ++i : ++i;


And this is clearly undefined right?
int k = ++i ? ++i : ++i + ++i ? ++i : ++i;

regards,
-fais

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]