News for hackers. Stuff that matters.
Re:Pitfalls in C and C++: Unsigned Types (2011) by elbee on Wednesday September 25, 2013 @02:59PM (#6446557)
On the other hand unsigned types are a huge pain if you want to iterate through an array backwards because you have to use subtraction. A lot of people end up with something like this:

  unsigned int i = strlen(s) - 1;
  for (; i >= 0; --i) { // BUGBUG
      if (s[i] == '.') {
          break;
      }
  }
(Yes, you can make it work, but it is very error-prone when people try).
Re:Pitfalls in C and C++: Unsigned Types (2011) by pslam on Wednesday September 25, 2013 @03:08PM (#6446617)
Simple transformation:

  for (unsigned i = strlen(s); i > 0; --i) {
    if (s[i - 1] == '.') break;
  }
Easy to see that s[i-1] does not underflow the array due to the loop invariant i>0. It's usually easy to convert signed iteration code to unsigned and when I see this, I can tell the author spent the time to consider what happens at the limits of their inputs.
Re:Pitfalls in C and C++: Unsigned Types (2011) by beagle3 on Thursday September 26, 2013 @12:34AM (#6448976)
gcc has been complaining about that one for the last 15 years or so, saying "condition is always true i >= 0".

Some errors are more subtle and not flagged by compilers, but many are.