Re: 0 < -1 !!
Re: 0 < -1 !!
- Subject: Re: 0 < -1 !!
- From: Nicko van Someren <email@hidden>
- Date: Fri, 23 Jul 2004 08:58:10 +0100
On 23 Jul 2004, at 3:48, Jerry LeVan wrote:
Just squandered the evening fiddling with code that looked like
...
return (currentIndex < [executedStatements count] - 1);
...
if I set int t1 = currentIndex and int t2 = [executedStatements count]
-1
and then returned ( t1 < t2) I got the right answer...
After squandering several hours ( it took a while to see what the
problem was).
I Read The Fine Manual and it told me that the count method returns an
"unsigned"
I guess I have to go back and review how expressions are evaluated.
The expression "a < b - c" will be interpreted as "a < (b - c)" due to
operator precedence, which is almost certainly what you want. If b and
c are both unsigned expressions the compiler will compute an unsigned
subtraction and give an unsigned result (which in this case will
underflow).
The fix was....
currentIndex < (int)[executedStatements count] - 1
This is the right way to solve the problem. For what it's worth, the
cast to type int here binds more tightly than the subtraction so you
are turning the count into a signed number and then carrying out a
signed subtraction, rather than (int)([executedStatements count] - 1),
which on most platforms would have given the same result but would
actually be performing an unsigned subtraction and then casting the
underflowed result back into a signed integer.
Cheers,
Nicko
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >0 < -1 !! (From: Jerry LeVan <email@hidden>) |