K
Kai-Uwe Bux
Please consider
#include <iostream>
int main ( void ) {
int a = 0;
a = ( (a = 5), 4 ); // (*)
std::cout << a << '\n';
}
I would like to determine whether line(*) has undefined behavior (modifying
a variable twice between sequence points).
What I think is this: We have an assignment expression
lhs = rhs
where the right hand side is this:
(a = 5), 4
I note that the comma will introduce a sequence point that separates the
side-effects of a=5 from the side-effects of 4. The main question therefore
is whether the right hand side
(a = 5), 4
can have a value before the side effects of its evaluation take place. My
understanding of the standard is that sequence points only separate side
effect but do not tell us anything about how, where and when values of
expressions are established. However, if that understanding is correct, the
abstract machine would be allowed to predict that the right hand side will
have value 4 and perform the side effect of the ambient assignment
lhs = rhs
before the side effects of the rhs take place. Along this possible path of
execution, the value of a would be modified twice. Thus, as of now, I
believe that line (*) has undefined behavior.
However, I am not sure about this interpretation of the standard; and I
would appreciate your help. (In fact, I hope that I am wrong.)
Note that similar considerations would apply to:
a = f();
Is it guaranteed that the side-effects of evaluating f() take place before
the value of a is changed? or would a conforming implementation allowed to
put all side-effects of f() on hold until it knows the value, perform the
assignment, and then run the side-effects of f()?
Thanks
Kai-Uwe Bux
#include <iostream>
int main ( void ) {
int a = 0;
a = ( (a = 5), 4 ); // (*)
std::cout << a << '\n';
}
I would like to determine whether line(*) has undefined behavior (modifying
a variable twice between sequence points).
What I think is this: We have an assignment expression
lhs = rhs
where the right hand side is this:
(a = 5), 4
I note that the comma will introduce a sequence point that separates the
side-effects of a=5 from the side-effects of 4. The main question therefore
is whether the right hand side
(a = 5), 4
can have a value before the side effects of its evaluation take place. My
understanding of the standard is that sequence points only separate side
effect but do not tell us anything about how, where and when values of
expressions are established. However, if that understanding is correct, the
abstract machine would be allowed to predict that the right hand side will
have value 4 and perform the side effect of the ambient assignment
lhs = rhs
before the side effects of the rhs take place. Along this possible path of
execution, the value of a would be modified twice. Thus, as of now, I
believe that line (*) has undefined behavior.
However, I am not sure about this interpretation of the standard; and I
would appreciate your help. (In fact, I hope that I am wrong.)
Note that similar considerations would apply to:
a = f();
Is it guaranteed that the side-effects of evaluating f() take place before
the value of a is changed? or would a conforming implementation allowed to
put all side-effects of f() on hold until it knows the value, perform the
assignment, and then run the side-effects of f()?
Thanks
Kai-Uwe Bux