Signed word lengths and indexes

Steven Schveighoffer schveiguy at yahoo.com
Tue Jun 15 08:34:16 PDT 2010


On Tue, 15 Jun 2010 10:05:08 -0400, Pelle <pelle.mansson at gmail.com> wrote:

> On 06/15/2010 03:25 PM, Adam Ruppe wrote:
>> On 6/15/10, Pelle<pelle.mansson at gmail.com>  wrote:
>>> for (uint i = end - 1; i<  uint.max; --i)
>>>
>>> Same functionality, really clever.
>>
>> That's wrong rather than clever though.
>>
>> for(i<  length) is saying "continue as long as you are inside the
>> array's bounds", which is exactly what you mean in that loop. The only
>> "tricky" is the understanding that an array index is never negative,
>> but this shouldn't require extra thought in the first place.
>
> Using the length is meaningless, any uint >= length will work just as  
> well. Using the length there is meaningless, since that's really not  
> what you compare against.

No, it's not.

for(uint i = initialize(); i < length; modify(i))

This construct is valid no matter what initialize or modify does to i.

-Steve


More information about the Digitalmars-d mailing list