Re: Accessing result of assignment

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 09/06/04


Date: Mon, 6 Sep 2004 14:40:49 -0400 (EDT)


On Mon, 6 Sep 2004, Barry Schwarz wrote:
>
> Could someone please explain what section 6.5.16-4 means
>
> "The order of evaluation of the operands is unspecified. If an attempt
> is made to modify the result of an assignment operator or to access it
> after the next sequence point, the behavior is undefined."
>
> I understand the order of evaluation part.
>
> d = b*b-4*a*c;
> if (d>0) ...
>
> This is pretty common code and obviously not undefined behavior if the
> variables are properly defined and assigned values. But it seems that
> the if statement attempts to access the result of the previous
> assignment operator after the end of statement sequence point as
> described in the quote. What am I misinterpreting?

   The result of the assignment operator is discarded. It never gets
used. What gets used in the 'if' statement is the value of the object
'd'. (That 'd' was assigned to in the previous statement has absolutely
no relevance.)

   The condition

     if ((d = b*b-4*a*c) > 0) ...

uses the result of the assignment operator in an expression, but it
uses it /before/ the next sequence point, so it's okay.

HTH,
-Arthur



Relevant Pages

  • Accessing result of assignment
    ... "The order of evaluation of the operands is unspecified. ... is made to modify the result of an assignment operator or to access it ... This is pretty common code and obviously not undefined behavior if the ... assignment operator after the end of statement sequence point as ...
    (comp.lang.c)
  • Re: What is a sequence point?
    ... > I know the basic definition of a sequence point (point where all side ... > "Between the previous and next sequence point an object shall have its ... > stored value modified at most once by the evaluation of an expression. ...
    (comp.lang.c)
  • Lazy evaluation: overloading the assignment operator?
    ... One particular use for this would be to implement "lazy evaluation". ... However, if we could overload the assignment operator, there would be ... Should there be a PEP to overload the assignment operator? ...
    (comp.lang.python)
  • Re: Accessing result of assignment
    ... > "The order of evaluation of the operands is unspecified. ... > is made to modify the result of an assignment operator or to access it ... > assignment operator after the end of statement sequence point as ... operator" is a value of type mystruct, but it is not a or b. ...
    (comp.lang.c)
  • Re: x=(x=5,11)
    ... Evaluating an assignment operator causes ... A side-effect is part of an expression evaluation. ... seem to be saying that the sequence point after c but before d means ... I don't see where he is saying that. ...
    (comp.lang.c)

Loading