Re: working with 8 and 16 bit regions of mem

From: nrk (ram_nrk2000_at_devnull.verizon.net)
Date: 03/18/04


Date: Thu, 18 Mar 2004 05:48:41 GMT

Mac wrote:

> On Wed, 17 Mar 2004 21:55:35 +0000, LaDainian Tomlinson wrote:
>
>> On 2004-03-17, 'Mac' <foo@bar.net> wrote in comp.lang.c:
>>
>> <snip>
>>
>>> int main(void)
>>> {
>>> struct byte ram[65536];
>>> struct address i;
>>> for (i.add=0; i.add > 0; i.add++)
>>> ram[i.add].data = 0;
>>> return 0;
>>> }
>>
>> <snip>
>>
>> Am I wrong in assuming that this loop will never run?
>
> No. I am feeling like an idiot, however.
>
>> If you initialize
>> i.add to 0, then i.add > 0 is immediately false. Perhaps:
>>
>> i.add = 0;
>> do {
>> ram[i.add++].data = 0;
>> } while (i.add > 0);
>>
>> I would prefer a more easily recognizable condition myself (e.g., i <
>> RAM_SIZE or something).
>
> I would say your preference is amply justified by my mistake. The only
> problem is, you can't compare with RAM_SIZE, because RAM_SIZE is always
> one larger than the largest possible value of the register. In other
> words, if RAM_SIZE is 65536, then the largest possible value of i.add
> would be 65535, so (i.add < RAM_SIZE) would always be true, and the loop
> would never terminate. Maybe just compare for less than or equality with
> RAM_SIZE - 1.
>
> i.e.,
>
> for (i.add=0; i.add <= RAM_SIZE - 1; i.add++)
> ram[i.add].data = 0;
>

Ummmm... Does that program halt?

-nrk.

PS: i < 1 is the same as i <= 0 :-)

>>
>> Brandan L.
>
> Anyway, thanks for the correction. It certainly highlights the problems
> associated with looping through every possible value of an unsigned type.
>
> --Mac

-- 
Remove devnull for email


Relevant Pages

  • Re: working with 8 and 16 bit regions of mem
    ... > Am I wrong in assuming that this loop will never run? ... I am feeling like an idiot, ... problem is, you can't compare with RAM_SIZE, because RAM_SIZE is always ...
    (comp.lang.c)
  • Re: Should function argument be changed in function body?
    ... usually better to initialize a loop variable right before ... length_of_string(const char *s){ ... > (I agree that modifying function parameters is not something ...
    (comp.lang.c)
  • Re: ccs compiler with pic12ce674?
    ... I modified the program to only toggle pin 5. ... You don't initialize it to ... > directive probably set it to 0 and nothing in your loop gets executed. ... > toggleState to TRUE so GP4 gets set high the first time through. ...
    (comp.arch.embedded)
  • RE: Decrementing row after deletion
    ... You might want to exit your second loop when you delete the row. ... "SueCool" wrote: ... I''''m not a complete idiot - there are some parts missing! ... If MyPos> 0 Then ...
    (microsoft.public.excel.programming)
  • Re: ATL OOM Resource leaks, and inconsistent behavior?
    ... I had the call to initialize the redemption object at the ... the initialization code so that it no longer initializes in any loop. ... >> You need the extra AddRef(). ...
    (microsoft.public.win32.programmer.messaging)