Re: working with 8 and 16 bit regions of mem
From: nrk (ram_nrk2000_at_devnull.verizon.net)
Date: 03/18/04
- Next message: Barry Schwarz: "Re: Accessing a member of a struct via various pointers"
- Previous message: nrk: "[OT] Re: what does it mean?"
- In reply to: Mac: "Re: working with 8 and 16 bit regions of mem"
- Next in thread: Mac: "Re: working with 8 and 16 bit regions of mem"
- Reply: Mac: "Re: working with 8 and 16 bit regions of mem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Barry Schwarz: "Re: Accessing a member of a struct via various pointers"
- Previous message: nrk: "[OT] Re: what does it mean?"
- In reply to: Mac: "Re: working with 8 and 16 bit regions of mem"
- Next in thread: Mac: "Re: working with 8 and 16 bit regions of mem"
- Reply: Mac: "Re: working with 8 and 16 bit regions of mem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|