是 i=f();当 f 修改 i 时定义?
相关问题: 赋值运算符不存在的任何充分理由序列点?
从 comp.lang.c FAQ 我会推断下面的程序是未定义的。奇怪的是,它只提到对 f
的调用作为参数计算和控制权转移到 f
之间的序列点。从 f
到调用表达式的控制转移未列为序列点。
int f(void) { i++; return 42; }
i = f();
真的是未定义吗?
作为我在许多问题中添加的尾注,我对静态分析的背景感兴趣。我不是自己写这个,我只是想知道我是否应该在其他人编写的程序中发出警告。
Related question: Any good reason why assignment operator isn't a sequence point?
From the comp.lang.c FAQ I would infer that the program below is undefined. Strangely, it only mentions the call to f
as a sequence point, between the computation of the arguments and the transfer of control to f
. The transfer of control from f
back to the calling expression is not listed as a sequence point.
int f(void) { i++; return 42; }
i = f();
Is it really undefined?
As an end-note that I add to many of my questions, I am interested in this in the context of static analysis. I am not writing this myself, I just want to know if I should warn about it in programs written by others.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。
你有一个 return 语句,因此,你有一个序列点。
它甚至没有出现
未定义的情况。 (这对我来说有点奇怪。)
Yes it is.
You have a return statement, therefore, you have a sequence point.
It doesn't even appear that
is undefined. (Which to me is kind of weird.)
这根本不是未定义的。 C99 附录 C 中列出的序列点之一是完整表达式的结尾,其中一个是 return 语句中的表达式。
由于您返回 42,因此紧跟在
return
语句之后有一个序列点。为了完整起见,这里列出了 C99 序列点,相关的序列点以粗体显示:
以下是 5.1.2.3 中描述的序列点:
说明符(7.19.6、7.24.2)。
也在对比较函数的任何调用和对象的任何移动之间
作为参数传递给该调用 (7.20.5)。
That's not undefined at all. One of the sequence points listed in Appendix C of C99 is the end of a full expression, of which one is the expression in a return statement.
Since you're returning 42, there's a sequence point immediately following that
return
statement.For completeness, the C99 sequence points are listed here, with the relevant one bolded:
The following are the sequence points described in 5.1.2.3:
specifier (7.19.6, 7.24.2).
also between any call to a comparison function and any movement of the objects
passed as arguments to that call (7.20.5).