6 ms·
> C says "undefined" so that compilers are free to simply let each CPU do whatever is easiest for that CPU. On any particular CPU with a particular compiler yo
by pascal_cuoq 13y ago
> C says "undefined" so that compilers are free to simply let each CPU do whatever is easiest for that CPU. On any particular CPU with a particular compiler you get consistent behavior. But when you switch CPUs, watch out.
That was how it worked in the 1990s. Nowadays, a C programmer needs to figure out which undefined behaviors are justifiable and which should be avoided at all costs because they will be used by the compiler to justify optimizations. And Signed arithmetic overflow used to be in the first category, now it is in the second one. So is the use of uninitialized variables.
Not to appeal to authority, but I worry about these things for a living:
http://blog.frama-c.com/index.php?post/2013/07/11/Arithmetic-overflows-in-Fluorine http://blog.frama-c.com/index.php?post/2013/07/11/Arithmetic...
http://blog.frama-c.com/index.php?post/2013/05/20/Attack-by-Compiler http://blog.frama-c.com/index.php?post/2013/05/20/Attack-by-...
If you cannot be bothered to read that much, then please simply compile int f(int x) { return x + 1 > x; } at different optimization levels with the compiler you already have, and observe the values for f(INT_MAX) in each case.
- btilly 13y agoThank you for the update to my knowledge. I'm not a C programmer, and have been relying on older explanations of why C does what it does.