4 ms·
No mention of undefined behavior? That's the first thing I tell people about C, considering how easy it is to trigger.
by akkartik 11y ago
No mention of undefined behavior? That's the first thing I tell people about C, considering how easy it is to trigger.
- charlesL 11y agoThe issue with undefined behavior is it's kind of hard to... well... define. It's an odd combination of unsafe memory, float/integer rollover, and rules with memory allocation. I wasn't quite sure how to clearly state it.
- akkartik 11y agoYeah I tend not to get hung up on the different ways it can trigger. It's more like, if you trigger it your program might run fine for now and then suddenly act up one day. It might die before the undefined behavior. It might launch nukes. Still sure you want to get into this? No? Want to go back to Ruby? Well, Ruby is built atop C. That's the five states of grief I try to get them through.
- kibibu 11y agoWorse than that - undefined behaviour can cause optimization to make your software insecure. http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html http://blog.llvm.org/2011/05/what-every-c-programmer-should-...
- caf 11y agoAll you really need to say is: There are some rules in C which neither the compiler nor the runtime is required to check if you've broken. On the contrary, the implementation is allowed to assume that you haven't broken them - which means that if you do break these rules, all bets are off as far as the behaviour of your program goes, which may lead to all sorts of strange and apparently inconsistent results.
- vortico 11y agoThere was a mention of garbage being returned from an out of bounds array.
- TheLoneWolfling 11y agoUnfortunately, 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 ago