4 ms·
Not where they are set to a constant. Just that they behave consistently. So, for example, x - x == 0 for int x, even if x is uninitialized. (Basically, it beh
by TheLoneWolfling 12y ago
Not where they are set to a constant.
Just that they behave consistently. So, for example, x - x == 0 for int x, even if x is uninitialized. (Basically, it behaves as though x was set to some value, although that value may vary from object to object and from run to run.)
- masklinn 12y ago> Just that they behave consistently. Erroring out because the variable is not initialised is consistent yes?
- TheLoneWolfling 12y agoYes, but erroring out on something like that is not good for other reasons. Among other things, that moves it from a local problem to a global problem. It's the same problem with undefined behavior in general. It's an example of a local problem causing global problems. You don't want your rocket to crash just because something threw an unchecked exception.
- masklinn 12y ago> You don't want your rocket to crash just because something threw an unchecked exception. Good news: the error is a compile-time error, not a runtime error.
- TheLoneWolfling 12y agoYou hope. And then you run into something like this [1] - which has been open for >10 years, and all of a sudden you're invoking UB without realizing it. [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=18501 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=18501
- pcwalton 12y agoI'm not aware of a language designed for programmers that works like this, but LLVM IR's undef works like that. http://llvm.org/docs/LangRef.html#undefined-values http://llvm.org/docs/LangRef.html#undefined-values
- TheLoneWolfling 12y agoNo it doesn't. For example, in LLVM "x - x" for int x isn't always zero if x is undef. (Skipping over syntax here) Quoting from the linked document: > This can be surprising to people (and also matches C semantics) where they assume that “X^X” is always zero, even if X is undefined. > the short answer is that an ‘undef‘ “variable” can arbitrarily change its value over its “live range”.
- pcwalton 12y agoOh, you actually want a live range to be allocated for the value. No, I'm not aware of any programming language that works like this (nor do I think the benefits would be worth it since you'd be increasing register pressure).
- TheLoneWolfling 12y agoI'm not so sure. The number of debugging hours that have been spent because of UB causing globally logically inconstant behavior... And it's better in terms of register pressure than the standard alternative - namely implicitly initializing everything.
- chongli 12y agoSo you want a language with a computer algebra system (CAS) that reduces expressions such as "x - x" at compile time. This has been doable in Lisp for a long time using macros. It'd be a bit trickier to do in a C-like language due to the syntax but still reasonable.
- TheLoneWolfling 12y ago