tricky assignment statemenent

C

ccwork

p = p->next = q;

Why it is undefined?
operator "=" is right-associative i.e. "p = p->next = q;" is
equivalent to "p = (p->next = q);
And I think another interpretation of sequence point is: between 2
sequence point a variable cannot be modified twice. In "p = p->next =
q;", "p->next" is modified once, "p" is modified once. (Let say we use
another pointer "k" such that "k=p->next" and then "p = p->next = q;"
become "p = k = q". Obviously the "k" and "p" are modified once
individually.)
 
C

Christian Bau

p = p->next = q;

Why it is undefined?
operator "=" is right-associative i.e. "p = p->next = q;" is
equivalent to "p = (p->next = q);
And I think another interpretation of sequence point is: between 2
sequence point a variable cannot be modified twice. In "p = p->next =
q;", "p->next" is modified once, "p" is modified once. (Let say we use
another pointer "k" such that "k=p->next" and then "p = p->next = q;"
become "p = k = q". Obviously the "k" and "p" are modified once
individually.)

There are no rules in C in which order operands are evaluated.

The value of q will eventually be stored into p. Will the p in "p->next"
be read before or after q is stored into p? If you find where in the C
Standard this is defined, then fine. Good luck looking for it.
 
M

mfhaigh

Christian said:
There are no rules in C in which order operands are evaluated.

The value of q will eventually be stored into p. Will the p in "p->next"
be read before or after q is stored into p?

Before. In some cases there are implicit rules about the relative
order of evaluation of expressions. Let's take the trivial example of:

p = p + 1;

According to your logic, this would be undefined. However, 6.5.2 gets
us off the hook:

6.5 Expressions
..
.. 2 Between the previous and next sequence point an object
.. shall have its stored value modified at most once by the
.. evaluation of an expression. Furthermore, the prior value
.. shall be accessed only to determine the value to be stored.

The expression 'p + 1' represents 'the value to be stored'. Within 'p
+ 1', p represents 'the prior value'. Here, p is 'accessed only to
determine the value to be stored'.

Simply mentioning 'the prior value' creates an implicit, partial
operand evaluation order. p must not be modified until its 'prior
value' is evaluated.

The expression:

p = p->next = q;

Is evaluated much the same as the above, and is defined behavior.
'p->next = q' is the 'value to be stored'. Since p is being modified
in the expression, the p in p->next is the 'prior value'. Since
p->next is a participant in the expression 'p->next = q' (the 'value to
be stored'), it's clear that 'the prior value [is] accessed only to
determine the value to be stored'.

It *is* defined behavior. Whether or not it is good style is another
matter altogether.


Mark F. Haigh
(e-mail address removed)
 
C

Christian Bau

The expression:

p = p->next = q;

Is evaluated much the same as the above, and is defined behavior.
'p->next = q' is the 'value to be stored'. Since p is being modified
in the expression, the p in p->next is the 'prior value'. Since
p->next is a participant in the expression 'p->next = q' (the 'value to
be stored'), it's clear that 'the prior value [is] accessed only to
determine the value to be stored'.

The prior value is most definitely _not_ only accessed to determine the
value to be stored. It is _also_ accessed to determine the address of
another memory location (&p->next) whose contents is also modified.
There are two assignments here, two memory locations that are modified.
How would you determine the second location without reading p?
It *is* defined behavior. Whether or not it is good style is another
matter altogether.

Absolutely not defined. Similar to "a = i++; ": i is modified. The
prior value is accessed to determine the new value stored in i. i is
also accessed for another purpose: to determine a memory location where
the value of i++ will be stored. Undefined behavior.
 
L

Lawrence Kirby

There are no rules in C in which order operands are evaluated.

There are for operators like && and ? :

However the issue here is not in what order operands are evaluated it is
about whether the operands have to be evaluated before the operation is
performed and a result produced. It is an issue of sequencing based on
data dependency.
The value of q will eventually be stored into p. Will the p in "p->next"
be read before or after q is stored into p? If you find where in the C
Standard this is defined, then fine. Good luck looking for it.

I've already explained this. :)

Lawrence
 
E

Eric Schmidt

Christian said:
The expression:

p = p->next = q;

Is evaluated much the same as the above, and is defined behavior.
'p->next = q' is the 'value to be stored'. Since p is being
modified in the expression, the p in p->next is the 'prior value'.
Since p->next is a participant in the expression 'p->next = q'
(the 'value to be stored'), it's clear that 'the prior value [is]
accessed only to determine the value to be stored'.

The prior value is most definitely _not_ only accessed to determine
the value to be stored. It is _also_ accessed to determine the
address ofanother memory location (&p->next) whose contents is
also modified. There are two assignments here, two memory
locations that are modified. How would you determine the
second location without reading p?

Yes, it is defined behavior. Consider

int i = 5;
/* ..... */
i = i*0;

Would you argue that this has undefined behavior?
It *is* defined behavior. Whether or not it is good style is
another matter altogether.

Absolutely not defined. Similar to "a = i++; ": i is modified.
The prior value is accessed to determine the new value stored in i.
i is also accessed for another purpose: to determine a memory
location where the value of i++ will be stored. Undefined behavior.


Here, i is both modified in the right-hand side and accessed in the
left-hand side. Since the access is not used to determine the stored
value (as it does not occur in the modifying expression) this is indeed
undefined behavior.
 
J

Jun Woong

Eric Schmidt said:
Christian Bau wrote: [...]
The prior value is most definitely _not_ only accessed to determine
the value to be stored. It is _also_ accessed to determine the
address ofanother memory location (&p->next) whose contents is
also modified. There are two assignments here, two memory
locations that are modified. How would you determine the
second location without reading p?

Yes, it is defined behavior. Consider

int i = 5;
/* ..... */
i = i*0;

Would you argue that this has undefined behavior?

In your example, "i" is accessed to determine the new value to be
stored into "i" and it's the *only* purpose for the access, so it's
defined. Which is quite different from the OP's tricky example. If you
are claiming that because "i" is multiplied by 0 one can determine
the value to be stored with no access to "i" and so the access to "i"
on the right-hand side is not to determine the to-be-stored value,
then what you think is completely irrelevant to the standard; it's
about optimization. According to your logic (assumed above), do you
really think that the following is well defined BY the standard?

int *p; /* not initialized */
*p = *p * 0;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top