return语句后顺序点? [英] Sequence point after a return statement?

查看:242
本文介绍了return语句后顺序点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我回答一个问题<一href=\"http://stackoverflow.com/questions/15617638/returning-a-variable-while-using-a-post-increment-in-c/15618319#15618319\">here我解释说,当后缀++是在一个全局变量用于在同一行作为收益发生了什么语句。

In my answer to a question here I explained what happened when postfix ++ was used on a global variable on the same line as a return statement.

C11的资料性附录C声明有一个后立即返回一个序列点,指的是规范的章节6.8.6.4,其中没有关于序列点文本可以是找到。

The informative appendix C of C11 states that there is a sequence point immediately after a return and refers to normative chapter 6.8.6.4, where no text regarding sequence points can be found.

我在哪里C标准中可以找到规范的文字说明,有一个收益语句?

Where in the C standard can I find normative text stating that there is a sequence point after a return statement?

(我只找到标准化文本,说明这个库函数,作为一个特殊的情况下,在7.1.4 / 3)。

(I only found normative text stating this for library functions, as a special case, at 7.1.4/3.)

推荐答案

Ç2011(n1570草案)6.8 4:每个下面的是一个完整的前pression:...(可选的)前pression在收益语句。有一个完整的前pression的评估和下一个完整的前pression进行评估的评估之间的序列点。

C 2011 (draft n1570) 6.8 4: "Each of the following is a full expression: … the (optional) expression in a return statement. There is a sequence point between the evaluation of a full expression and the evaluation of the next full expression to be evaluated."

所以技术上顺序点是不是以后的收益,但除权pression的在收益评估和下前pression之间。考虑这个code,调用时 A 最初为0:

So technically the sequence point is not after a return but is between the evaluation of the expression in the return and the next expression. Consider this code, called when a is initially 0:

int a = 0;

int Foo(void) { return a++; }

void Bar(void)
{
    int b = Foo() + a;
    …
}

美孚()+ A ,无论美孚() A 首先计算是不确定的。 收益对的回归前pression之间的序列点并在下一个完整的前$ P后,我们会考虑这两个订单的两个潜规则光(序列点$ pssion)。如果不实施 A 第一,那么就必须做到:

In Foo() + a, whether Foo() or a is evaluated first is unspecified. We will consider both orders in light of both potential rules (sequence point after return versus sequence point between the expression of the return and the next full expression). If the implementation does a first, then it must do:

a
Sequence point
Foo()
+

然后其他一些全EX pression将跟随,因此,无论是通过规则,就不会有一个顺序点,而这个code是相同无论哪种方式,只要我们关注。其结果是, B 设置为0。

如果实现确实美孚()第一,那么,随着规则一收益后序指针,实现必须做到:

If the implementation does Foo() first, then, with the "sequence pointer after a return" rule, the implementation must do:

Sequence point
Foo()
Sequence point
a
+

这code将定义的行为: A 是由副作用在递增,这是前完成一个进行访问,那么 + 执行。其结果是, A 设置为1。虽然结果可能是0或1,这种后序点的收益的规则,这是只是不确定这两个命令的使用;该行为是不是完全不确定的。

This code would have defined behavior: a is incremented by the side effect in Foo, and that is complete before a is accessed, then + is performed. The result is that a is set to 1. Although the result may be 0 or 1 with this "sequence point after return" rule, it is merely unspecified which of the two orders is used; the behavior is not completely undefined.

不过,如果实现确实美孚()第一,使用序列点标准C规则的前pression之间的返回并在下一个完整的前pression,那么我们有:

However, if the implementation does Foo() first and uses the standard C rule of "sequence point between the expression of a return and the next full expression", then we have:

Sequence point
Foo()
???
a
???
+
???

在???标志的地方,所需要的序列点可能在返回后所随处之前下一个完整的前pression。在这种情况下, A 的值可能会在 A 并修改富来访问( ),并没有干预序列点。这是不确定的行为。

The "???" mark places where the required sequence point might be—anywhere after the return and before the next full expression. In this case, the value of a might be accessed in a and modified in Foo(), and there is no intervening sequence point. That is undefined behavior.

因此​​,规则的回归前pression后序点之前下一个完整的前pression一个后立即返回从顺序点不同;第一有在本实施例中未定义的行为,而第二不

Therefore, the rule "sequence point after the expression of a return and before next full expression" is different from "sequence point immediately after a return"; the first has undefined behavior in this example, and the second does not.

这篇关于return语句后顺序点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆