5 ms·
Unsigned int >= 0 in a decrementing for-loop is a classic trap
by kdoherty 12y ago
Unsigned int >= 0 in a decrementing for-loop is a classic trap
- bgvopg 12y agoSome would twitch at this: for(i = length - 1; i < SIZE_MAX ; i--) but it is completely valid.
- anon4 12y agojust fucking use for(i = 0; i < length; i++) .. do something with (length - i - 1) .. I've been much happier since I started writing all loops as incrementing loops.
- cbsmith 12y agofor (i = 0; i <= length; i++) .. boy I sure hope length is not a max value .. It's not about the incrementing vs. decrementing so much as the equality bounded loop on a value that could be a min/max value. Gets you every time.
- astrange 12y agoAssuming, of course, i is size_t and not int.
- bgvopg 12y agoMy reply to kdoherty isn't assuming that. Read the parent carefully.
- heinrich5991 12y agoI don't see it, can you explain why this works with `int`?
- Ded7xSEoPKYNsDd 12y agoIt doesn't work for int, but kdoherty specified unsigned int.
- joosters 12y agoThe twitchers are right. Whoever wrote that line of code is a smartass who is deliberately obfuscating stuff. Don't work with these people!
- bgvopg 12y agoI prefer to learn, rather than stay ignorant, when I find new tech. The only people I would not want to work with are ignoramuses and ironically, smart-asses. So you.