Re: Ambiguity in semantics of assignments?

From: Mark F. Haigh (mfhaigh_at_sbcglobal.net)
Date: 10/08/04


Date: 7 Oct 2004 17:23:55 -0700

steck@ccs.neu.edu (Paul Steckler) wrote in message news:<9bc41fd7.0410061304.dd833f7@posting.google.com>...
> Here's some code that's giving me differing results, depending
> on the compiler.

<snip>

> A = foo.A += A; /* crucial statement */
>

<snip>

> The result given by gcc is what I expected. Is there any
> ambiguity in the semantics of the crucial statement here?
>

No, there's not. 6.5.16:

 3 An assignment operator stores a value in the object designated
     by the left operand. An assignment expression has the value of
     the left operand after the assignment, but is not an lvalue.
     The type of an assignment expression is the type of the left
     operand unless the left operand has qualified type, in which
     case it is the unqualified version of the type of the left
     operand. The side effect of updating the stored value of the
     left operand shall occur between the previous and the next
     sequence point.

The value of the += in 'foo.A += A' is well defined, even though the
state of the foo.A object itself is indeterminite until the next
sequence point. It is the value of the +=, not the value of foo.A,
that should be stored in A. This may be where the compiler gets it
wrong.

As you noted in another post, this is a compiler bug, and from the
looks of it, a relatively severe one. Hmmm.

Mark F. Haigh
mfhaigh@sbcglobal.net



Relevant Pages

  • Re: Evolution of PL/I
    ... In this case the arithmetic value the string operand represents is converted to the attributes that a FIXED DECIMALoperand would have been converted in the same context, where M is the maximum allowable fixed decimal precision. ... The CALL/OS compiler is somewhat simpler than the F compiler, and does FIXED DECIMAL in binary. ...
    (comp.lang.pl1)
  • Re: [C, C++] "(a=b) = c;" Illegal or Undefined?
    ... What I am saying is that a compiler might look ... order of evaluation of the left and right operand is unspecified. ... occurs somewhere during the previous and next sequence point. ... assignment and 4 after assignment. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Multiple Assignment Evaluation Debate
    ... have no right to expect anything in particular, because a compiler ... "int", it will still have 3 in it later. ... An assignment operator stores a value in the object ... designated by the left operand. ...
    (comp.lang.c)
  • Re: ||= operator
    ... On Mon, Aug 10, 2009 at 11:22, Bryan R ... All of that is fine if you are reading perlop from start to finish, ... the left operand is true, the right operand is not even evaluated. ...
    (perl.beginners)
  • Re: x=(x=5,11)
    ... there is a sequence point between the ... without invoking undefined behavior. ... the value of the right operand is converted ... convert it to the type of the assignment expression without evaluting ...
    (comp.lang.c)