gpt4 book ai didi

c++ - 这是 C/C++ 中的未定义行为吗

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:18 24 4
gpt4 key购买 nike

int foo(int c){
return c;
}

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

它会调用 C/C++ 中的未定义行为吗?我认为不会。

看完所有的答案我都搞不清楚是未定义行为还是未指定行为。

最佳答案

是的,这是未定义的行为 - afoo(--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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com