3 ms·
Unfortunately, UB extends far beyond that. To the point of being (very) logically inconsistent. People not familiar with C would naively expect that, for insta
by TheLoneWolfling 11y ago
Unfortunately, UB extends far beyond that. To the point of being (very) logically inconsistent.
People not familiar with C would naively expect that, for instance, `x[10] == x[10]` is always true even if 10 is out-of-bounds for x (or rather: it may crash, or it may be true.) . But compilers can - and will - assume that this situation is false if it makes the code faster.
This sort of thing is my major pain point with C - you quite literally have to know the entire code to make any judgements about any one piece of code, even trivial ones. You end up having to fight the compiler at every turn.
- missblit 11y agoNot only can the compiler make it false. It can assume that the code would never make the comparison in the first place, and optimize away any cases where the undefined behavior is guaranteed to be triggered. So in a sense undefined behavior can travel back in time :O
- TheLoneWolfling 11y agoYep. It's impossible to have actual modular code in C. A real shock for people coming from higher level languages.
- xcogiz 11y agoWhat exactly do you mean by modular code in your context? edit: Oh, you probably meant modules integrated directly in the language. I was surprised to read that since modular design is highly successful and useful in C.
- TheLoneWolfling 11y agoNo, I'm not talking about modules. I'm talking about modular code: I mean that in many languages it's at least possible to write code that can be understood fully in isolation. Objected-oriented programming, etc, etc. In C that's impossible. Due to UB being global.