O
ozbear
Someone was asking in another forum how to translate:
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language.
I am at odds with myself as to whether this causes undefined behaviour
in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?
In otherwords, the simple statement:
y = (y+y1)/2;
is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?
Or, is it ok since /y/ isn't actually used in the larger
expression but the well-defined value assigned to it?
My question would equally apply, I think, to something like...
x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */
Regards, Oz
if (x && (y1=(y+y1)/2)) /* all integers with defined values */
to another language.
I am at odds with myself as to whether this causes undefined behaviour
in the first place. Does the subexpression (y1=(y+y1)/2)
cause UB because /y/ is both modified and used without an
intervening sequence point attached to the larger expression
or is it ok because the value of the assignment is the left
hand side is used?
In otherwords, the simple statement:
y = (y+y1)/2;
is well defined assuming reasonable values for y and y1 but when
used in a larger expression as above does it become UB since
/y/ is now both modified and used?
Or, is it ok since /y/ isn't actually used in the larger
expression but the well-defined value assigned to it?
My question would equally apply, I think, to something like...
x = y = (y+y2)/2; /* ok, not ok, or bad analogy? */
Regards, Oz