3 ms·
> it always leads to UB, which means you can't say anything about what happens next. If you read a language standard and try very hard to forget that the actua
by nananana9 8mo ago
> it always leads to UB, which means you can't say anything about what happens next.
If you read a language standard and try very hard to forget that the actual computer exists, sure.
If you remember computers are real, you can pretty easily tell what will happen when you write to address 0x00000000 on a CPU with virtual memory.
- FartyMcFarter 8mo agoNot all memory bugs result in writing to a null pointer. For example, you can do a double free, or write to a pointer that was freed.
- Tuna-Fish 8mo agoDo 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 8mo agoAre there any languages other than C and C++ that have this “nasal demons” interpretation of undefined behavior?
- FartyMcFarter 8mo agoI think so, at least when it comes to assuming that multi-threading data races don't happen.
- josephg 8mo 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 8mo ago[deleted]
- bregma 8mo agoAre you asking if there are programming languages in which how undefined behaviour behaves is strictly defined?
- xigoi 8mo 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 8mo agoRust and Zig do, and I think also Go.
- IshKebab 8mo agoAh you're in the "but it doesn't really mean anything can happen" denial stage. Welcome to acceptance: https://mohitmv.github.io/blog/Shocking-Undefined-Behaviour-In-Action/ https://mohitmv.github.io/blog/Shocking-Undefined-Behaviour-...