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

标签 c++ c undefined-behavior

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/

相关文章:

c++ - 在 travis for clang 上使用不同的 libc++ 版本

c - 二叉树实现: issues with insert_tree()

c++ - 给定一个数及其根值,求一个数的 n 次方根

c - 从C中的链表中删除第一个元素

c++ - 移位运算符如何在 C++ 中处理负数

C++/海湾合作委员会 : segmentation fault at exit of main after catching a signal

c++ - 从 OpenMP 循环内部访问和编辑 aN ma 时出错

c++ - 当 first <= last 不满足时,C++ 标准对算法有何规定?

c++ - 元组的一个元素可以引用另一个元素吗?

c++ - 如何使用 Python C/API 包装多线程 C++ 库?