c中的序列点 [英] sequence points in c

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

问题描述

命令式编程中的序列点定义了计算机程序执行中的任何点,在该点上可以保证先前评估的所有副作用都已执行,并且后续评估的副作用尚未执行.

A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.

这是什么意思?谁能用简单的语言解释一下?

What does this mean? Can somebody please explain it in simple words?

推荐答案

当一个序列点出现时,基本上意味着你保证之前的所有操作都完成了.

When a sequence point occurs, it basically means that you are guaranteed that all previous operations are complete.

在没有中间序列点的情况下更改变量两次是未定义行为的一个示例.

Changing a variable twice without an intervening sequence point is one example of undefined behaviour.

例如,i = i++; 是未定义的,因为 i 的两次更改之间没有序列点.

For example, i = i++; is undefined because there's no sequence point between the two changes to i.

请注意,不仅仅是将变量更改两次会导致问题.这实际上是涉及任何其他用途的更改.在讨论事物的排序方式时,该标准使用术语价值计算副作用".例如,在表达式a = i + i++中,i(值计算)和i++(副作用)可以以任意方式完成订购.

Note that it's not just changing a variable twice that can cause a problem. It's actually a change involved with any other use. The standard uses the term "value computation and side effect" when discussing how things are sequenced. For example, in the expression a = i + i++, the i (value computation) and i++ (side effect) may be done in arbitrary order.

维基百科在 C 和 C++ 标准中有一个序列点列表,尽管是最终列表应始终取自 ISO 标准.来自 C11 附录 C(释义):

Wikipedia has a list of the sequence points in the C and C++ standards although the definitive list should always be taken from the ISO standard. From C11 appendix C (paraphrased):

以下是标准中描述的序列点:

The following are the sequence points described in the standard:

  • 在函数调用和实际调用中函数指示符和实际参数的计算之间;
  • 在运算符 &&|| 的第一个和第二个操作数的计算之间;
  • 在条件 ?: 运算符的第一个操作数的计算与第二个和第三个操作数中的任何一个计算之间;
  • 完整声明符的结束;
  • 在完整表达式的求值和下一个要求值的完整表达式之间.以下是完整的表达:
    • 一个初始化器;
    • 表达式语句中的表达式;
    • 选择语句的控制表达式(ifswitch);
    • while 或 do 语句的控制表达式;
    • for 语句的每个表达式;
    • 返回语句中的表达式.
    • Between the evaluations of the function designator and actual arguments in a function call and the actual call;
    • Between the evaluations of the first and second operands of the operators &&, ||, and ,;
    • Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated;
    • The end of a full declarator;
    • Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions:
      • an initializer;
      • the expression in an expression statement;
      • the controlling expression of a selection statement (if or switch);
      • the controlling expression of a while or do statement;
      • each of the expressions of a for statement;
      • the expression in a return statement.

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

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