做评估括号力秩序,使一个未定义的前pression界定? [英] Do parentheses force order of evaluation and make an undefined expression defined?

查看:122
本文介绍了做评估括号力秩序,使一个未定义的前pression界定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正要虽然我的课本,当我遇到这个问题。

来了

  1. 下面前pression后会是什么的价值?结果
    假设的一个= 5.Mention的步骤的初始值

    • A + =(A ++)+(++一)


起初我以为这是不确定的行为,因为已修改多次。结果
于是我读了问题,它说的提到的步骤,所以我大概以为这个问题是对的。

所以我的问题是:


  • 申请是否使括号中定义的未定义的行为?

  • 是评价一个括号前pression?
  • 之后创建一个序列点
  • 如果它被定义,请问自++括号物质和()具有相同的precedence

注意:一个很好的解释和明确的答案会得到我的投票


解决方案

没有,应用括号不会让它定义的行为。它仍然不确定。 C99标准和教派; 6.5第2说:


  

在previous和下一个序列点的对象应具有其之间
  由前pression评价修改最多一次存储的值。
  此外,前一个值是只读确定该值
  要被存储


在括号把一分前pression可能迫使子前pressions评价的顺序,但它并没有创建一个序列点。因此,它不能保证当子前pressions的副作用,如果它们产生任何,将发生。再次与教派引述C99标准; 5.1.2.3与第2


  

一个前pression评估可能产生的副作用。在某些
  在执行顺序称为序列点,所有指定点
  previous评估的副作用,应完整,无副作用
  后续评估的作用应发生。


为了完整起见,以下是通过附件C中的C99标准规定的序列点。


  

      
  1. 对函数的调用,后面的变量进行了评估。


  2.   
  3. 以下运算符的第一个操作的结束:逻辑和&放大器;&安培; ;逻辑或 || ;有条件的 ?;逗号


  4.   
  5. 一个完整的声明符的末尾。


  6.   
  7. 一个完整的前pression的结束;恩pression在离pression声明;一个选择语句(控股前pression 如果
      或开关);控股前pression一个,而
      声明;每个语句前pressions的;该
      在返回前pression 语句。


  8.   
  9. 之前的一个库函数返回。


  10.   
  11. 与每个格式的输入/输出功能转换说明相关的动作后。


  12.   
  13. 在马上,而每次调用比较函数之后,以及任何电话之间的一个比较函数和任何
      作为参数传递给该调用传递的物体的运动。


  14.   

I was just going though my text book when I came across this question

  1. What would be the value of a after the following expression ?
    Assume the initial value of a = 5.Mention the steps
    • a+=(a++)+(++a)

At first I thought this is undefined behaviour because a has been modified more than once.
So then I read the question and it said Mention the steps so I probably thought this question is right.

So my question is :

  • Does applying parentheses make an undefined behaviour defined ?
  • Is a sequence point created after evaluating a parentheses expression ?
  • If it is defined,how does the parentheses matter since ++ and () have the same precedence

Note: A well explained and clear answer will get my vote

解决方案

No, applying parentheses doesn't make it a defined behaviour. It's still undefined. The C99 standard §6.5 ¶2 says

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

Putting a sub-expression in parentheses may force the order of evaluation of sub-expressions but it does not create a sequence point. Therefore, it does not guarantee when the side effects of the sub-expressions, if they produce any, will take place. Quoting the C99 standard again §5.1.2.3¶2

Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.

For the sake of completeness, following are sequence points laid down by the C99 standard in Annex C.

  1. The call to a function, after the arguments have been evaluated.

  2. The end of the first operand of the following operators: logical AND &&; logical OR ||; conditional ?; comma ,.

  3. The end of a full declarator.

  4. The end of a full expression; 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.

  5. Immediately before a library function returns.

  6. After the actions associated with each formatted input/output function conversion specifier.

  7. Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call.

这篇关于做评估括号力秩序,使一个未定义的前pression界定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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