开发者

Is there a sequence point between these assignments?

Is there a sequence point bet开发者_开发技巧ween the two assignments in the following code:

f(f(x=1,1),x=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


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜