Sequence points when calling functions in C and undefined/unspecified behaviour

隐身守侯 提交于 2019-12-04 11:24:09

No, per 6.5.2.2 10 there is no sequence point between the evaluation of subexpression arguments, just before the actual call.

One way of looking at it is that it is unspecified whether the behaviour is undefined; if the implementation sequences the two ++i subexpressions before any call to g or h then the behaviour is undefined, but if the ++i subexpressions are evaluated as late as possible (immediately before calling g and h respectively) then the behaviour is unspecified. However, because the implementation is always at liberty to choose between any allowed unspecified behaviour then the overall result is undefined.

Incorrect. Sequence points specify a partial order on the allowed ordering of operations. In case (2), there are sequence points:

  1. At the semicolon at the end of the line (1)
  2. After the evaluation of the arguments of g (i.e. the ++i) but before the call to g
  3. After the evaluation of the arguments of h (i.e. the ++i) but before the call to h
  4. After the evaluation of the arguments of f (i.e. after f and g have returned) but before the call to f
  5. After the return from f

So the partial order looks like this, from top to bottom:

    1
   / \
  /   \
 2     3
  \   /
   \ /
    4
    |
    | 
    5

2 and 3 are not ordered with respect to each other, since the order of evaluation of arguments is unspecified. Since i gets modified twice between the sequence points 1 and 4, the behavior is undefined.

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