4 ms·
Your for loop works, whereas the one in the parent comment would run forever, correct? Is there something I'm missing here? edit: Ok, looking at it again the p
by renlo 10y ago
Your for loop works, whereas the one in the parent comment would run forever, correct? Is there something I'm missing here?
edit: Ok, looking at it again the parent example is probably going to overflow or something right?
- cygx 10y agoYour for loop works, whereas the one in the parent comment would run forever, correct? The other way around: size_t is an unsigned type, so decrementing 0 will wrap around to SIZE_MAX, a value that is positive as well as greater than 9. This means counter to your intuition, the first loop will terminate and the second one won't.
- clarkcox3 10y agoThe example with ">= 0" is an infinite loop, a size_t will always be greater than or equal to zero; it's unsigned.
- imron 10y ago> Is there something I'm missing here? Yes (see other replies), and it's precisely the reason why this code shouldn't pass code review. At first glance the first example looks like it should fail but in fact works. The second example I provided looks like it should work, but in fact loops infinitely. The first one is tricky and nifty, but prone to bugs. Using signed counters is better way to go about it.