在C / C这个未定义行为++ [英] Is this undefined behavior in C/C++

查看:162
本文介绍了在C / C这个未定义行为++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int foo(int c){
    return c;
}

int main(void){
    int a=5,c;
    c = foo(--a) + a; 
}

它会在调用C / C未定义行为++?我觉得没有也不会。

Will it invoke undefined behavior in C/C++? I think no it won't.

看完所有的答案后,我无法弄清楚它是否是未定义的行为或不确定的行为。

After reading all the answers I can't figure out whether it is undefined behavior or unspecified behavior.

推荐答案

是的,它是不确定的行为 - A 美孚( - A)可以以任何顺序进行评价。

Yes it's undefined behavior - a and foo(--a) can be evaluated in any order.

有关进一步的参考,例如见序列点。有完整的前pression后顺序点,和参数的评估后 - 但SUBEX pressions的计算顺序是不确定的,每5/4:

For further reference, see e.g. Sequence Point. There's a sequence point after the complete expression, and after evaluation of the argument to foo - but the order of evaluation of subexpressions is unspecified, per 5/4:

除非另有说明,秩序
  个人操作数的评价
  运营商和SUBEX pressions
  个别前pressions,以及订单
  在这种副作用的发生,是
  不确定的。在previous之间
  而下一个序列点标
  对象应具有其存储的值
  在由最多一次修改
  一个前pression的评价。
  此外,前值应为
  只访问,以确定该值
  要被存储。此要求
  段应满足每
  的容许排序
  SUBEX $一个完整的前pression的对$ pssions;
  否则,行为是不确定的。

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

编辑:Prasoon指出,行为的未指定的由于评估...的顺序是不确定的,然后变成的未定义由于前一个值只能访问,以确定该值将被存储

As Prasoon points out, the behavior is unspecified due to the order of evaluation ... is unspecified., and becomes undefined due to the prior value shall be accessed only to determine the value to be stored

这篇关于在C / C这个未定义行为++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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