delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/28/10:37:39

From: Andrew Crabtree <andrewc AT typhoon DOT rose DOT hp DOT com>
Message-Id: <199706281437.AA160338651@typhoon.rose.hp.com>
Subject: Re: Newbie for loop/data type problem
To: csantill AT lausd DOT k12 DOT ca DOT us (csantill)
Date: Sat, 28 Jun 1997 7:37:31 PDT
Cc: djgpp AT delorie DOT com
In-Reply-To: <33B4CBA2.40D6@lausd.k12.ca.us>; from "csantill" at Jun 28, 97 1:30 am

> Ok, I know chars are byte values, but
> they seemed to be signed in DJGPP
I think they are signed in most compilers.  GCC gives an option to
make them unsigned as you mention.

>  But, w/the unsigned char flag on, I get an infinite
> loop
Um, you should get an infinite loop with it off also.

> If anybody has a work around (while loop, a mix of C wrapped
> in an inline ASM loop) or any other help would be great;
          ^^^^^^^^^^
Nothing so drastic.

 
> Here's a vague description of my code:
> 
> void stupid_error(char get_some_stupid_vars)
> {
> 
>   char c;
> 
>   for(c=0;c<=255;c++)
>   {
>     /* do some stupid newbie stuff */
>   }
> 
> }
> 
OK - lets assume that the char datatype is an unsigned 8 bit value.
That means the largest number it can store is 255.  Look what happens
in your code as the variable hits 255.  Test for <=, yep 255<=255.  Now,
increment.  Here it will roll to 0.  Now test for <=, yep 0 <= 255.  
Instant infinite loop.

Possible solutions - 

Use an int data type.  
Change the conditions of your loop

Andrew

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019