3 ms·
Imagine you have a function that traverses a non-circular linked list. You implement it with a recursive function. The compiler uses Tail Call Optimization to c
by UncleMeat 4y ago
Imagine you have a function that traverses a non-circular linked list. You implement it with a recursive function. The compiler uses Tail Call Optimization to convert it to a loop. Then your program hands this function a circularly linked list.
Your program now behaves differently after optimization. Before, your program crashed with a stack overflow. Now, your program loops forever.
Should the compiler have not done this? Or is it a bug in your program that you passed an invalid input to this function?
- xigoi 4y agoLooping forever is the same as if you ran the recursive version on a hypothetical machine with unlimited stack memory, so it didn't really change semantics.
- UncleMeat 4y agoBut we aren't running on a hypothetical machine. That's the whole argument with the "define everything" crowd. Those two programs are different on my machine. Is that my fault or the compiler's fault? People say "I just want my machine to do what my machine does when I dereference a pointer at address 0, why is my compiler making my program do something different?" Why can't I say the same thing in my case?
- xigoi 4y agoNo, the argument against undefined behavior is that it makes errors silent. I don't care whether dereferencing a null pointer causes a segfault, an exception or something else, but it shouldn't cause the program to run as if nothing is wrong.
- UncleMeat 4y agoIn a multithreaded program, one of the implementations I described crashes my program and one makes everything else but the stuck thread run just fine.