int foo(int c){
return c;
}
int main(void){
int a=5,c;
c = foo(--a) + a;
}
它会调用 C/C++ 中的未定义行为吗?我认为不会。
看完所有的答案我都搞不清楚是未定义行为还是未指定行为。
最佳答案
是的,这是未定义的行为 - a
和 foo(--a)
可以按任何顺序求值。
如需进一步引用,请参阅Sequence Point .在完整表达式之后以及对 foo
的参数求值之后有一个序列点 - 但根据 5/4,子表达式的求值顺序未指定:
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 指出的那样,由于评估顺序......未指定,行为未指定,并变为未定义 由于仅应访问先前值以确定要存储的值
关于c++ - 这是 C/C++ 中的未定义行为吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5389411/