4 ms·
> like how older code uses "int" as the counter for every single for loop, despite this being an incredibly stupid type for this purpose Is it? I always intern
by sokoloff 5y ago
> like how older code uses "int" as the counter for every single for loop, despite this being an incredibly stupid type for this purpose
Is it? I always internalized int as “the most natural sized number for the platform”. For a lot of uses, that’s a pretty reasonable choice, despite there more specific/constrained types available now.
- seoaeu 5y agoThe problem is that the "most natural sized number for the platform" was 32-bits for so long on major platforms, that now everyone assumes that changing that will break too much for it to be worth switching
- cpeterso 5y agoint is signed and signed int overflow is UB. If you use an int as a loop counter, the compiler can optimize the loop assuming the loop counter will never overflow because, surely, a programmer would never invoke UB. :) If the loop counter does overflow, then who knows what bad things will happen. For example, a simple loop like “for (int i = 0; i >= 0; i++) { … }” might become an infinite loop, crash, execute random code, or never execute in the first place.
- sokoloff 5y agoI think it's defined to execute at least until overflow, is it not? (So, even assuming a 1-bit int, it's going to execute at least once I think.)
- witty_username 5y agoNo, if your program ever invokes UB, anything could happen (even at the beginning). In other words, UB can time travel.
- sokoloff 5y agoIf the {…} includes a break, undefined behavior could well be avoided.
- msbarnett 5y agoIts not about the size, its about the signedness being presumably not natural for an index, and the potential for having very inconvenient consequences for performance on modern CPU architectures if you were not able to assume the index never goes negative due to a wrap. Basically, to get decent perf out of C loops, arithmetic overflow has to be left as UB for int. If the standard index variable type had historically been unsigned and defined to terminate on overflow or something, you could make wrap-on-overflow standard for int without killing the performance of hot inner loops.
- saurik 5y agoNo: the killer issue is the size. The loop index going negetive doesn't affect the performance at all, but making sure that it actually wraps at 32-bits instead of 64-bits requires simulating the 32-bit wrapping with a clamp.
- msbarnett 5y agoloop optimization is intimately tied to being able to assume the index doesn't wrap and exit the array, no? GCC says: > -faggressive-loop-optimizations This option tells the loop optimizer to use language constraints to derive bounds for the number of iterations of a loop. This assumes that loop code does not invoke undefined behavior by for example causing signed integer overflows or out-of-bound array accesses. The bounds for the number of iterations of a loop are used to guide loop unrolling and peeling and loop exit test optimizations.