www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Array length unsigned..

reply "Regan Heath" <regan netwin.co.nz> writes:
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
Aug 17 2005
next sibling parent reply zwang <nehzgnaw gmail.com> writes:
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)
Aug 17 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
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
Aug 17 2005
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
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.
Aug 27 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
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
Aug 28 2005