Is there a sequence point between the two assignments in the following code:
f(f(x=1,1),x=2);
Is there a sequence point between the two assignments in the following code:
f(f(x=1,1),x=2);
The relevant quote from the (draft) standard [6.5.2.2, 10] is:
The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
So for your expression, the first argument (in particular the call to f
) could be
evaluated before the second argument; e.g.:
(x = 1, 1), f <sp> call, (x = 2), f <sp> call
Or, it could be evaluated after the second argument; e.g.:
(x = 2), (x = 1, 1), f <sp> call, f <sp> call
[The function call itself can (and most probably will) contain more sequence points (in particular if it contains a return
statement).]
Depending on that, there is a sequence point between the assignments or not. It is up to the platform ("unspecified").
Since in the 2nd case, you are assigning to x
twice between two sequence points, you have undefined behaviour on such a platform.
No there isn't. The standard is indeed ambiguous in this case.
If you want to confirm that, gcc has this really cool option -Wsequence-point
and in this case it will warn you that the operation may be undefined
x=2; f(x, x=1)
. Even though the x=1 is to the right of the original x, the function call is f(1,1)
–
Coxalgia x=1
is evaluated first and hence the f(1,1)
. Maybe read my previous comment again. –
Lovettalovich f(x=1,x)
does the same thing –
Coxalgia f(x=1, x)
on my g++, the result was f(1, 2)
. It would have been really really odd if both of them did the same thing. I mean, whether left-to-right or right-to-left, both of them CAN'T have the same results! –
Lovettalovich f(1,1)
for both cases. –
Coxalgia x
that is underdetermined in this code block. There are circumstances where the lack of specificity here is irrelevant (for example, if the expression were f(f(x=1,1),y=2);
, it wouldn't matter if y=2
occurs before or after the x=1
). –
Coxalgia There are sequence points at the beginning of a function call and at it's end. However because the order of operations on function arguments is implementation defined you can't gaurantee that f(x=1,1)
will be executed before x=2
.
Also note that the ,
in the function call case is not the comma operator that introduces a sequence point.
The relevant quote from the (draft) standard [6.5.2.2, 10] is:
The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
So for your expression, the first argument (in particular the call to f
) could be
evaluated before the second argument; e.g.:
(x = 1, 1), f <sp> call, (x = 2), f <sp> call
Or, it could be evaluated after the second argument; e.g.:
(x = 2), (x = 1, 1), f <sp> call, f <sp> call
[The function call itself can (and most probably will) contain more sequence points (in particular if it contains a return
statement).]
Depending on that, there is a sequence point between the assignments or not. It is up to the platform ("unspecified").
Since in the 2nd case, you are assigning to x
twice between two sequence points, you have undefined behaviour on such a platform.
There is a sequence point, but the order of evaluation (and their side effects) of the outer function's arguments is still undefined. The implementation is free to first evaluate the inner f(), with its side effect x=1, or the second argument with its side effect x=2.
Yes, because there is a sequence point before and after function calls.
§1.0.17 of the Standard says:
When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body. There is also a sequence point after the copying of a returned value and before the execution of any expressions outside the function).
f
is evaluated first. –
Euphorbia Yes there will be a sequence point due to comma operatror But still result will be undefined as evaluation of function arguments is undefined so can't predict what value this expression will generate........means undefined behaviour
© 2022 - 2024 — McMap. All rights reserved.
strtol(s++, &s, 0)
, though it was fixed in later gcc versions) so I'm hesitant to rely on it. – Candlewood