I know that when you do certain things in a C program, the results are undefined. However, the compiler should not be generating invalid (machine) code, right? It would be reasonable if the code did the wrong thing, or if the code generated a segfault or something...
Is this supposed to happen according to the compiler spec, or is it a bug in the compiler?
Here's the (simple) program I'm using:
int main() {
char *ptr = 0;
*(ptr) = 0;
}
I'm compiling with -O3
. That shouldn't generate invalid hardware instructions though, right? With -O0
, I get a segfault when I run the code. That seems a lot more sane.
Edit: It's generating a ud2
instruction...