Hacker News new | ask | show | jobs
by chowells 331 days ago
Actually, 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.

1 comments

Yeah, 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++;
  }