Re: Multiple Assignment Evaluation Debate



ena8t8si@xxxxxxxxx wrote:

pete wrote:
ena8t8si@xxxxxxxxx wrote:

Richard G. Riley wrote:
"ena8t8si@xxxxxxxxx"posted the following on 2006-03-10:


Richard Heathfield wrote:
berns@xxxxxxxxxxxx said:

Hi All,

A coworker and I have been debating
the 'correct' expectation of
evaluation for the phrase a = b = c.
Two different versions of GCC
ended up compiling this as b = c; a = b
and the other ended up
compiling it as a = c; b = c.
(Both with no optimizations enabled).
How did we notice you may ask?
Well, in our case 'b' was a memory
mapped register that only has a select number
of writable bits. He
claims it has been a 'C Standard'
that it will be evaluted as a = c; b
= c. I personally believe that it
would make more sense for it to be
evaluated as b = c; a = b,
although I would never write code that has a
questionable operation. Can anyone settle this debate?


The point of interest is that
"assignment" is not a sequence point.

If
sorted -> next = *node;
sorted = *node;
is defined, then
sorted = sorted -> next = *node;
is undefined.

Even if that ridiculous claim were true,

The ridiculous claim is true.
The topic comes up on this newsgroup from time to time,
usually in the form of "p = p->next = q"
You can look it up next time you're online.

http://groups.google.com/group/comp.lang.c/search?group=comp.lang.c&q=%22p+%3D+p-%3Enext+%3D+q%22

http://groups.google.com/group/comp.lang.c/msg/1c7e2102fd256d79

"The p = p->next = q problem is also a real problem that came from
a program I was writing when I posted a question about it here
years ago." -- Ben Pfaff

it isn't responsive
to the OP's question. Does the intermediate assignment
influence what value is assigned to a? The answer to that
is YES.

No.
The value of the intermediate assignment
influences what value is assigned to a,
but that value is known prior to the side effect
of the actual assignment taking place.

If you have
unsigned char uc;
unsigned int ui;
ui = uc = -1;
that can be translated as either
uc = UCHAR_MAX;
ui = UCHAR_MAX;
or
ui = UCHAR_MAX;
uc = UCHAR_MAX;
The order in which the side effects take place doesn't matter.

OP's question is entirely an issue of
side effects not being ordered between sequence points.

--
pete
.



Relevant Pages

  • Re: Multiple Assignment Evaluation Debate
    ... Richard Heathfield wrote: ... If you depend on the order of evaluation, any side effects of a, b, and c ... If b is volatile that may also change the picture, ... "assignment" is not a sequence point. ...
    (comp.lang.c)
  • Re: Multiple Assignment Evaluation Debate
    ... Richard Heathfield wrote: ... If you depend on the order of evaluation, any side effects of a, b, and c ... are likely to bite you eventually. ... the intermediate assignment to b can ...
    (comp.lang.c)
  • Re: Strange operands to conditional operator
    ... has higher precedence than an assignment operator, ... order of evaluation. ... There is a sequence point after the first operand. ...
    (comp.lang.c)
  • Re: Must an expression be evaluated before its value is used?
    ... this is not a sequence point. ... The evaluation dependency has a consequence of ... seperate the assignment of 5 to x from the assignment of 11 to x. ... comma operator without first evaluating it's second operand. ...
    (comp.std.c)
  • Re: Multiple Assignment Evaluation Debate
    ... Vladimir S. Oka wrote: ... The compiler _knows_ that the ... the assignment is evaluated and assigned to p. ... the compiler _is_ obliged to do the evaluation as per ...
    (comp.lang.c)