www.digitalmars.com         C & C++   DMDScript  

D - ArrayBoundsError

reply "alexander.panek" <alexander.panek brainsware.org> writes:
hi,

there is a notice in the D reference, that this is not possible:

	int[] foo;
	int	i;

	foo[0] = 1;	
	foo[1] = 2;
	foo[2] = 3;
	foo[3] = 4;
	.
	.

	i = foo[3];

first, why causes this an error and second, why leads this into an error 
too:
	
	//XML is my class
	XML _xml;
	int i;	
	_xml = new XML(args[i]);

how could i program this without any arrayboundserror?

thx, alex

(p.s. sorry for my bad english.)
-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Sep 07 2003
parent reply Mike Wynn <mike l8night.co.uk> writes:
alexander.panek wrote:
 hi,
 
 there is a notice in the D reference, that this is not possible:
 
     int[] foo;
foo is 'null'
     int    i;
 
     foo[0] = 1;   
you cant do this as foo is null (foo.length == 0)
     foo[1] = 2;
     foo[2] = 3;
     foo[3] = 4;
     .
     .
 
     i = foo[3];
try foo.length = 4; (foo now has 4 elements). {0..3} or foo ~= 1; foo ~= 2; ..etc.. you can ~= (append) to a null array
 
 first, why causes this an error and second, why leads this into an error 
 too:
     
     //XML is my class
     XML _xml;
     int i;   
     _xml = new XML(args[i]);
 
 how could i program this without any arrayboundserror?
set i to a value! did you want to pass an element of args or the whole array ?
 
 thx, alex
 
 (p.s. sorry for my bad english.)
Sep 07 2003
parent reply Farmer <itsFarmer. freenet.de> writes:
Mike Wynn <mike l8night.co.uk> wrote in news:bjg6lu$1164$1 digitaldaemon.com:

 alexander.panek wrote:
 hi,
 
 there is a notice in the D reference, that this is not possible:
 
     int[] foo;
foo is 'null'
     int    i;
 
     foo[0] = 1;   
you cant do this as foo is null (foo.length == 0)
     foo[1] = 2;
     foo[2] = 3;
     foo[3] = 4;
     .
     .
 
     i = foo[3];
try foo.length = 4; (foo now has 4 elements). {0..3} or foo ~= 1; foo ~= 2; ..etc.. you can ~= (append) to a null array
I think this won't work: Since foo is null you cannot append or resize it. One could either create a static array: int[4] foo; int i; foo[0] = 1; foo[1] = 2; foo[2] = 3; foo[3] = 4; or use a dynamic array, that is initially empty (but not null). int[] foo=new int[0]; int i; foo ~= 1; foo ~= 2; foo ~= 3; foo ~= 4;
Sep 08 2003
parent reply Mike Wynn <mike l8night.co.uk> writes:
Farmer wrote:
 Mike Wynn <mike l8night.co.uk> wrote in news:bjg6lu$1164$1 digitaldaemon.com:
 
 
alexander.panek wrote:

hi,

there is a notice in the D reference, that this is not possible:

    int[] foo;
foo is 'null'
    int    i;

    foo[0] = 1;   
you cant do this as foo is null (foo.length == 0)
    foo[1] = 2;
    foo[2] = 3;
    foo[3] = 4;
    .
    .

    i = foo[3];
try foo.length = 4; (foo now has 4 elements). {0..3} or foo ~= 1; foo ~= 2; ..etc.. you can ~= (append) to a null array
I think this won't work: Since foo is null you cannot append or resize it. or use a dynamic array, that is initially empty (but not null). int[] foo=new int[0]; int i; foo ~= 1; foo ~= 2; foo ~= 3; foo ~= 4;
have you tried; int[] foo; foo ~= 1; or even foo = new int[0]; if ( foo === null ) { printf( "foo is null"); } just try ...... import c.stdio; int main( char[][] args ) { int[] foo; int[] bar; foo = null; // force null; foo ~= 1; printf( "foo.length=%d\n", foo.length ); printf( "foo[0] = %d\n", foo[0] ); bar = new int[0]; if ( bar === null ) { printf( "bar is null\n"); } return 0; } -------- on linux I get foo.length=1 foo[0] = 1 bar is null (the same as I used to get on windows) if your going to post at least try out some code!
Sep 08 2003
next sibling parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Mike Wynn" <mike l8night.co.uk> wrote in message
news:bjiu9j$207p$1 digitaldaemon.com...
| if your going to post at least try out some code!
|

At least lately (one week, maybe two) we haven't had any C++ code.
I agree with Mike: when we post code ideas we should try to do it in D, or
some sort of pseudo D at least. It'll even help yourselves: you'll become
familiar with, and if you make any mistakes, maybe someone will correct it.
Also, when new people come they'll see more D code and will notice we're
using it.
Also, it's good to try to give a "D flavour" to the code. eg: Daniel said it
was better to use ~= instead of += in I-don't-remember-exactly-what-post.
Also ("also" day for me :) ) try the compiler: "does D support this? does D
support that?". Try it! (no offence intended)

-------------------------
Carlos Santander
"Mike Wynn" <mike l8night.co.uk> wrote in message
news:bjiu9j$207p$1 digitaldaemon.com...
| if your going to post at least try out some code!
|

At least lately (one week, maybe two) we haven't had any C++ code.
I agree with Mike: when we post code ideas we should try to do it in D, or
some sort of pseudo D at least. It'll even help yourselves: you'll become
familiar with, and if you make any mistakes, maybe someone will correct it.
Also, when new people come they'll see more D code and will notice we're
using it.
Also, it's good to try to give a "D flavour" to the code. eg: Daniel said it
was better to use ~= instead of += in I-don't-remember-exactly-what-post.
Also ("also" day for me :) ) try the compiler: "does D support this? does D
support that?". Try it! (no offence intended)

-------------------------
Carlos Santander
Sep 08 2003
prev sibling parent Farmer <itsFarmer. freenet.de> writes:
Mike Wynn <mike l8night.co.uk> wrote in news:bjiu9j$207p$1 digitaldaemon.com:

 if your going to post at least try out some code!
 
I'm sorry, I had better done so. You're right, your code works fine. DMD doesn't care whether you write int[] foo = new int[0]; or int[] foo; Both produces the same code (even if optimization is turned off). I'm not happy with the current situation: 1)I can't distinguish between an empty array and a null array. Which is sometimes handy. 2)Arrays/Slices seem a bit erratic with regard to beeing null or empty. Here's an example: void main() { int[] foo = new int[0]; if (foo === null) printf("foo is null\n"); int[] foo2 = new int[1]; int[] emptySlice = foo2[0..0]; if (emptySlice !== null) printf("emptySlice is NOT null\n"); emptySlice.length = 0; if (emptySlice === null) printf("emptySlice is now null\n"); } prints: foo is null emptySlice is NOT null emptySlice is now null
Sep 09 2003