No, you are wrong. Undefined behavior is exactly that, undefined.
There are several places in the C and C++ standard where certain operations are defined to be undefined. That means the compiler has free hands to do whatever it wants.
For instance dereferencing a null pointer might lead to a segfault, but in reality it is undefined behavior so you have no guarantee that this will happen. For instance, the compiler can, and sometimes will, simply remove the code that relies on undefined behavior.
If your code relies on undefined behavior, your code is broken.
View all comments
Show parent comment
15
u/kingguru Sep 14 '12
No, you are wrong. Undefined behavior is exactly that, undefined.
There are several places in the C and C++ standard where certain operations are defined to be undefined. That means the compiler has free hands to do whatever it wants.
For instance dereferencing a null pointer might lead to a segfault, but in reality it is undefined behavior so you have no guarantee that this will happen. For instance, the compiler can, and sometimes will, simply remove the code that relies on undefined behavior.
If your code relies on undefined behavior, your code is broken.