Bugs of GCC

· · 科技·工程

原帖:https://zhaojinxi.blog.uoj.ac/blog/8996

#include <cstdio>
struct Node {
    int x;
};
Node f(int A, int B) {
    if (A < 0) return {0};
    return {A / B + (A % B != 0)};
}
Node g(int C, int D) {
    if (C > 0) return {0};
    return {C / D + (C % D != 0)};
}
int main() {
    int n;
    scanf("%d", &n);
    f(1, 1);
    Node t = g(-1, n);
    printf("%d\n", t.x);
}

输入 0,正确输出应为 1

这份代码在开启 O2 优化后输出 0

相关 bug report:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113907