← Back to context

Comment by anfilt

3 years ago

This probably just an example, but why are they using a signed int for indexing???

They're correct to do this. Signed integers have more UB, therefore you /should/ use them in all situations when overflow isn't going to happen, because you're not going to need that extra defined behavior.

This lets you use UBSan most effectively and it's what the Google style guide says to do.

(Exception if you care about micro-performance: * / >> operations can be a little faster on unsigned types IIRC)

I would also prefer unsigned indexes but exactly because the compiler may assume that there will be no overflow, signed index access may be a bit faster and therefore preferable.

  • On most machines this days there is not really a performance difference between the math done on signed or unsigned integers. The only case would be if your wanting to the compiler to optimize on the fact that UB does exist. So like this in example "impossible things" get optimized out. The author here clearly does not want that.

  • Is there a reason why the language doesn't provide UB-on-overflow (and wrappring overflow) for both unsigned and signed types?

    It always feels dirty deliberately using a signed type for something you know can never be neagtive just because that signed type has other properties you want.