Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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?



Your 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.


> 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.


The example with ">= 0" is an infinite loop, a size_t will always be greater than or equal to zero; it's unsigned.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: