Using size_t to crash on off-by-one errors (was: size_t vs long.)

2022-11-23 Thread Alejandro Colomar via Gcc
Hi, On 11/18/22 00:04, Alejandro Colomar wrote: The main advantage of this code compared to the equivalent ssize_t or ptrdiff_t or idx_t code is that if you somehow write an off-by-one error, and manage to access the array at [-1], if i is unsigned you'll access [SIZE_MAX], which will definite

Re: size_t vs long.

2022-11-17 Thread Alejandro Colomar via Gcc
Hi Paul, On 11/17/22 22:39, Paul Eggert wrote: Second and more important, that code is bogus. Nobody should ever write code like that. If I wrote code like that, I'd *want* a trap. for (size_t i = 41; i < sizeof A / sizeof A[0]; --i) {    A[i] = something_nice; } The code above seems a bug by