GCC:溢出的未定义行为是否应该保持逻辑一致性?

Mic*_*aël 0 c gcc overflow undefined-behavior integer-arithmetic

以下代码在我的系统上产生奇怪的东西:

#include <stdio.h>

void f (int x) {
  int y = x + x;
  int v = !y;
  if (x == (1 << 31))
    printf ("y: %d, !y: %d\n", y, !y);
}

int main () {
  f (1 << 31);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译-O1,这打印y: 0, !y: 0.

现在,除了删除int vif行产生预期结果的令人费解的事实之外,我对溢出转换为逻辑不一致的未定义行为感到不舒服。

这应该被认为是一个错误,还是 GCC 团队的理念是,一个意外的行为可以级联成逻辑矛盾?

Mar*_*lli 7

当调用未定义的行为时,任何事情都可能发生。毕竟,它被称为未定义行为是有原因的。

Should this be considered a bug, or is the GCC team philosophy that one unexpected behavior can cascade into logical contradiction?

It's not a bug. I don't know much about the philosophy of the GCC team, but in general undefined behavior is "useful" to compiler developers to implement certain optimizations: assuming something will never happen makes it easier to optimize code. The reason why anything can happen after UB is exactly because of this. The compiler makes a lot of assumptions and if any of them is broken then the emitted code cannot be trusted.

As I said in another answer of mine:

Undefined behavior means that anything can happen. There is no explanation as to why anything strange happens after invoking undefined behavior, nor there needs to be. The compiler could very well emit 16-bit Real Mode x86 assembly, produce a binary that deletes your entire home folder, emit the Apollo 11 Guidance Computer assembly code, or whatever else. It is not a bug. It's perfectly conforming to the standard.