Is there a sequence point between these assignments?

陌路散爱 提交于 2019-12-04 16:42:48

问题


Is there a sequence point between the two assignments in the following code:

f(f(x=1,1),x=2);

回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.




回答5:


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).




回答6:


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



来源:https://stackoverflow.com/questions/7356573/is-there-a-sequence-point-between-these-assignments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!