Hacker News new | past | comments | ask | show | jobs | submit login

Care to present some proof? Here’s an counter proof that the compiler isn’t able to reason about the memory in that way https://godbolt.org/z/x7j8xoMxY

There are cases where C can do loop hoisting, but the cases are a subset of what Rust does and this isn’t one of those.




You example doesn't show this. Also the allocation is hoisted out of the loop. The initialization is not and this would be invalid in general. It could eliminated in this case, but this would be dead store elimination.


That’s identical to what happens on Rust. The allocation of the underlying uninitialized array is done once (it’s just the stack space allocation) and it assigns the value 0 within the body. It does indeed show exactly that what OP is proposing is something that C isn’t capable of and it’s what the entire article is about - the inability to skip the initialization in the first place by reasoning that it’s not needed.


Except it would be invalid to move the initialization out of the loop, so the example does not show whether a compiler could do this in principle.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: