Consider this program:
#include <stdio.h> int main(void) { int x; while ( 1 == scanf("%d", &x) ) printf("%c\n", "hello"[x]); }
The compiler must compile this successfully, because the program does not have UB until the user enters any numbers outside the range 0 - 4 .
However, according to this topic , UB may go back in time. Now consider this program:
int main(void) { printf("hello\n"); "hello"[6]; }
Any call to this program leads to undefined behavior, and since it can move in time, all the behavior of this program for any call to undefined. Can a compiler refuse a program and not generate an executable file? (We could say that UB returns on time to the compilation stage!)
c ++ c undefined-behavior language-lawyer
MM Dec 19 '14 at 9:51 2014-12-19 09:51
source share