Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Quick: how would you implement for (i = n; i >= 0; i -= k) with unsigned integers?

I just use a signed pointer-size type for all sizes and counters, and stay happy...



Your loop:

    void TestSigned(int min, int max, int step) {
      for (int i = max; i >= min; i -= step) F(i);
    }
Becomes this with unsigned:

    void TestUnsigned(unsigned min, unsigned max, unsigned step) {
      while (true) {
        F(max);
        if (max < min + step) break;
        max -= step;
      }
    }
Now if I want to transform my loop to operate on pointers or iterators, the transformation is trivial:

    void TestUnsigned(unsigned *min, unsigned *max, unsigned step) {
      while (true) {
        F(*max);
        if (max < min + step) break;
        max -= step;
      }
    }
Quick: Do the same for yours.


It looks easy but it isn't.

The second example causes unexpected behavior (and probably ub later) if: min + step > MIN_TYPE_MAX

The last example causes undefined behavior if: max - min < step - 1.


Something like for(size_t i = 0; i <= n; i += k) { work_with(n - i); }?

This sort of loop always comes up in these discussions, and I was never convinced that signed integers were worth it for that alone.


To be honest I think there are good arguments to both sides, and I don't think there is really a one size fits all solution here. If you use unsigned you've got to pay attention to certain corner cases, if you use signed you've got to pay attention to different corner cases, it's up to you to decide which is more common in your code.


If n is not within k of the maximum unsigned integer:

  i = n+k;
  do {
    i -= k;
    ...
  } while (i >= k);
If n is close enough to the maximum unsigned integer for n+k to overflow:

  i = n;
  goto skip_sub;
  do {
    i -= k;
  skip_sub:
    ...
  } while (i >= k);




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

Search: