4 ms·
Do note that with modern compilers it's surprisingly hard to accidentally do something that is always guaranteed to write to 0. Because it is UB, and an optimiz
by Tuna-Fish 7mo ago
Do note that with modern compilers it's surprisingly hard to accidentally do something that is always guaranteed to write to 0. Because it is UB, and an optimizing compiler is allowed assume that it doesn't happen. This can lead to seemingly crazy things like a variable that is set to zero, and when you deref through it it gives you something completely different instead. Because if a variable is first set to zero in all code paths, and then complex logic usually sets it to something else, after which it is dereferenced, the compiler is allowed to notice that the path where it is accessed without being first set to something else never happens, and then it is allowed to notice that the first write to the variable is dead because it's never read before being set to something else, and thus can be eliminated.
- xigoi 7mo agoAre there any languages other than C and C++ that have this “nasal demons” interpretation of undefined behavior?
- FartyMcFarter 7mo agoI think so, at least when it comes to assuming that multi-threading data races don't happen.
- josephg 7mo agoI assume this is a product of sufficiently advanced compilers. Other LLVM languages almost certainly suffer from this too, including Zig, Swift and unsafe rust.
- deleted 7mo ago[deleted]
- bregma 7mo agoAre you asking if there are programming languages in which how undefined behaviour behaves is strictly defined?
- xigoi 7mo agoI’m talking about the “non-local” flavor of undefined behavior. For example, I’d expect the following code: int x; printf("%d", x - x); to always print 0, but it can it fact do anything at all.
- IshKebab 7mo agoRust and Zig do, and I think also Go.