home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 241,361 of 243,242   
   Keith Thompson to Keith Thompson   
   Re: signed vs unsigned and gcc -Wsign-co   
   20 Oct 25 17:13:03   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   Keith Thompson  writes:   
   [...]   
   > The thing about unsigned types is that they have a discontinuity at   
   > 0, which is much easier to run into than signed int's discontinuties   
   > at INT_MIN and INT_MAX.  Subtraction in particular can easily yield   
   > mathematically incorrect results for unsigned types (unless your   
   > problem domain actuall calls for modular arithmetic).   
      
   One specific footgun enabled by unsigned types involves loops that count   
   down to zero.  This :   
      
       for (int i = N; i >= 0; i --) {   
           // ...   
       }   
      
   is well behaved, but this :   
      
       for (size_t i = N; i >= 0; i --) {   
           // ...   
       }   
      
   is an infinite loop.  A compiler might warn that `i >= 0` is always   
   true.  You can work around this by checking the condition inside   
   the body of the loop, before the decrement that causes a wraparound :   
      
       for (size_t i = N; /* i >= 0 */; i --) {   
           // ...   
           if (i == 0) break;   
       }   
      
   But if your loop counts up, this isn't an issue.   
      
   "You too may be a big hero   
   Once you've learned to count backwards to zero."   
       -- Tom Lehrer, "Wernher Von Braun"   
      
   --   
   Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com   
   void Void(void) { Void(); } /* The recursive call of the void */   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca