Loop variable reaching integer `huge` causes infinite loop

I’ll go off on a tangent here, in C++ there is a lot of confusion among programmers on what type to chose for loop variables, signed or unsigned:

for (int i = 0; i < N; ++i) { /* ... */ }
// vs
for (size_t i = 0; i < v.size(); ++i) { /* ... */ }

The second form is often chosen because the sizes returned by STL containers are unsigned and compilers will issue a warning (false positive) about inconsistent types otherwise. In many cases range-based for loops, iterators, or STL algorithms can be used to get rid of the explicit loops entirely.

To keep the language simple, proposals from Bjarne Stroustrup suggest that signed integers (int) are the most natural loop variables (follow the links in this older post). In Fortran, where arrays can have negative lower bounds, they make sense too.

The smaller range (1 lost bit) feels like you’ve lost a lot of space, but if you’re problem is already close to 4 billion items, you might as well use a larger type and save yourself the trouble later if/when the size of your data increases.