| ▲ | foltik 11 hours ago |
| Ok, so you’re saying it’s “technically correct?” Practically speaking, I’d argue that a compiler assuming uninitialized stack or heap memory is always equal to some arbitrary convenient constant is obviously incorrect, actively harmful, and benefits no one. |
|
| ▲ | publicdebates 10 hours ago | parent | next [-] |
| In this example, the human author clearly intended mutual exclusivity in the condition branches, and this optimization would in fact destroy that assumption. That said, (a) human intentions are not evidence of foolproof programming logic, and often miscalculate state, and (b) the author could possibly catch most or all errors here when compiling without optimizations during debugging phase. |
| |
| ▲ | foltik 10 hours ago | parent [-] | | Regardless of intention, the code says this memory is uninitialized. I take issue with the compiler assuming anything about the contents of that memory; it should be a black box. | | |
| ▲ | masklinn 10 hours ago | parent [-] | | The compiler is the arbiter of what’s what (as long as it does not run afoul the CPU itself). The memory being uninitialised means reading it is illegal for the writer of the program. The compiler can write to it if that suits it, the program can’t see the difference without UB. In fact the compiler can also read from it, because it knows that it has in fact initialised that memory. And the compiler is not writing a C program and is thus not bound by the strictures of the C abstract machine anyway. | | |
| ▲ | foltik 9 hours ago | parent [-] | | Yes yes, the spec says compilers are free to do whatever they want. That doesn’t mean they should. > The user didn’t initialize this integer. Let’s assume it’s always 4 since that helps us optimize this division over here into a shift… This is convenient for who exactly? Why not just treat it as a black box memory load and not do further “optimizations”? | | |
| ▲ | masklinn 9 hours ago | parent [-] | | > That doesn’t mean they should. Nobody’s stopping you from using non-optimising compilers, regardless of the strawmen you assert. | | |
| ▲ | foltik 9 hours ago | parent [-] | | As if treating uninitialized reads as opaque somehow precludes all optimizations? There’s a million more sensible things that the compiler could do here besides the hilariously bad codegen you see in the grandparent and sibling comments. All I’ve heard amounts to “but it’s allowed by the spec.” I’m not arguing against that. I’m saying a spec that incentivizes this nonsense is poorly designed. | | |
| ▲ | Negitivefrags 5 hours ago | parent [-] | | Why is the code gen bad? What result are you wanting? You specifically want whatever value happened to be on the stack as opposed to a value the compiler picked? |
|
|
|
|
|
|
|
| ▲ | 1718627440 9 hours ago | parent | prev [-] |
| Also even without UB, even for a naive translation, a could just happen to be 13 by chance, so the behaviour isn't even an example of nasal demons. |