Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22159

GCC: Should undefined behavior of overflows preserve logical consistency?

$
0
0

The following code produces strange things on my system:

#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;
}

Compiled with -O1, this prints y: 0, !y: 0.

Now beyond the puzzling fact that removing the int v or the if lines produces the expected result, I'm not comfortable with undefined behavior of overflows translating to logical inconsistency.

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


Viewing all articles
Browse latest Browse all 22159

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>