Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 17, 2005 Array length unsigned.. | ||||
---|---|---|---|---|
| ||||
import std.stdio; void main() { char[][] tmp; tmp ~= "1"; tmp ~= "2"; //add these, no problem //tmp ~= "3"; //tmp ~= "4"; //tmp ~= "5"; for(int i = 0; i < tmp.length-3; i += 3) { writefln(tmp[i],",",tmp[i+1],",",tmp[i+2]); } } The code above causes an array bounds error. This occurs because tmp.length-3 == 4294967295 instead of -1. There are no errors or warnings about comparing an int 'i' with a uint 'tmp.length'. Regan |
August 17, 2005 Re: Array length unsigned.. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote:
> import std.stdio;
>
> void main()
> {
> char[][] tmp;
> tmp ~= "1";
> tmp ~= "2";
> //add these, no problem
> //tmp ~= "3";
> //tmp ~= "4";
> //tmp ~= "5";
> for(int i = 0; i < tmp.length-3; i += 3)
> {
> writefln(tmp[i],",",tmp[i+1],",",tmp[i+2]);
> }
> }
>
> The code above causes an array bounds error. This occurs because tmp.length-3 == 4294967295 instead of -1.
> There are no errors or warnings about comparing an int 'i' with a uint 'tmp.length'.
>
> Regan
The problem never occured to me because I usually write like this:
for(int i = 0; i+3 < tmp.length; i += 3)
|
August 17, 2005 Re: Array length unsigned.. | ||||
---|---|---|---|---|
| ||||
Posted in reply to zwang | On Wed, 17 Aug 2005 20:40:23 +0800, zwang <nehzgnaw@gmail.com> wrote:
> Regan Heath wrote:
>> import std.stdio;
>> void main()
>> {
>> char[][] tmp;
>> tmp ~= "1";
>> tmp ~= "2";
>> //add these, no problem
>> //tmp ~= "3";
>> //tmp ~= "4";
>> //tmp ~= "5";
>> for(int i = 0; i < tmp.length-3; i += 3)
>> {
>> writefln(tmp[i],",",tmp[i+1],",",tmp[i+2]);
>> }
>> }
>> The code above causes an array bounds error. This occurs because tmp.length-3 == 4294967295 instead of -1.
>> There are no errors or warnings about comparing an int 'i' with a uint 'tmp.length'.
>> Regan
>
> The problem never occured to me because I usually write like this:
>
> for(int i = 0; i+3 < tmp.length; i += 3)
Thanks. I'll probably change it to something like that.. I had recoded it to:
int len = tmp.length;
for(int i = 0; i < len-3; i += 3)
Regan
|
August 27, 2005 Re: Array length unsigned.. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | These kinds of problems cannot be solved by juggling around the promotion rules, conversion rules, etc. They just shift and manifest themselves in some other way. The only way to deal with it is to be wary of code that might cross 0 or int.max boundaries. |
August 28, 2005 Re: Array length unsigned.. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | On Sat, 27 Aug 2005 14:33:57 -0700, Walter <newshound@digitalmars.com> wrote: > These kinds of problems cannot be solved by juggling around the promotion > rules, conversion rules, etc. They just shift and manifest themselves in > some other way. The only way to deal with it is to be wary of code that > might cross 0 or int.max boundaries. There appears to be no warning (when using -w) when comparing signed and unsigned, eg. void main() { uint i = 4; int j = 6; if (i < j) {} } C:\Library\D\src\temp>dmd -w signed.d C:\Library\D\dmd\bin\..\..\dm\bin\link.exe signed,,,user32+kernel32/noi; Perhaps you could add one? Regan |
Copyright © 1999-2021 by the D Language Foundation