5 ms·
Isn't i going to be one past end on the last iteration? I think you want either "++i <= end" or "i++ < end", and that's assuming "end" is inclusive. If it's ex
by Izkata 1y ago
Isn't i going to be one past end on the last iteration?
I think you want either "++i <= end" or "i++ < end", and that's assuming "end" is inclusive. If it's exclusive then "++i < end", right?
(Using "<=" in a termination condition has become a code smell for me, it's been wrong more often than not, so it sticks out as "pay close attention here")
- chowells 1y agoActually, I think what you really need is "i++ != end". The whole point was inequalities don't work how you want when end is INT_MAX. You want to just terminate after the loop body where i was equal to end. Except now I'm not sure if that evades the UB of a signed int exceeding INT_MAX. I don't know C well enough to know if the UB is in performing the increment or reading the value afterwards. Edit: Just checked in with a C expert. The UB is in the increment operation, so that's not correct after all. You really do just need to separate out the update from the test entirely.
- trealira 1y agoYeah, it would have to be something like this if you wanted to avoid the signed integer overflow at the end of the loop: assert(i <= j); for (;;) { do_something_with(i); if (i == j) break; i++; }