3 ms·
> there is a (slightly) saner interpretation: branch there to execute the case, then "return" 10 from the statement expression and compare that to i, continuing
by jjnoakes 10y ago
> there is a (slightly) saner interpretation: branch there to execute the case, then "return" 10 from the statement expression and compare that to i, continuing onward from there as usual [...] and that does not cause any uninitialised reads or writes
The code you wrote did one additional thing: it moved the "switch" starting point from before the "i=0" initialization to after the "i=0" initialization, which is more than just a different interpretation, it's completely different semantics.
- yorwba 10y agoi is initialized twice in the example code int i = 0; switch(i){ for(i = 0; i < ({case 0:; 10;}); i++){ The compiler probably tried to be intelligent and optimized it away. EDIT: Okay I tried reading the assembly and I think I understand whats going on: movl $0, -8(%rbp) # translation of i = 0 ... # the following is jumped over movl -8(%rbp), %eax movl %eax, -16(%rbp) # 4-byte Spill # i is moved to somewhere else in memory ... # the switch jumps between these movl -16(%rbp), %eax # 4-byte Reload # tries to reload i from its "new" position Whoever wrote the code to place variables into memory obviously didn't consider this kind of insane control flow.
- jjnoakes 10y agoYep, you are right, I missed that "i" was initialized twice; when I see "i=0" in a for loop I assume it isn't initialized right above (otherwise, why put the "i=0" in the for loop at all?).