noob Q: array out-of-range
Don
nospam at nospam.com
Fri May 28 13:21:50 PDT 2010
Duke Normandin wrote:
> Hey...
>
> Compiler is now working as expected! Thank's to a great catch by
> Michel Fortin!
>
> A little further down the tutorial "D_ A Newbie-Oriented Tutorial",
> there's this code:
>
> [code]
> import std.stdio;
>
> void main()
> {
> int[] intArray;
>
> intArray[0] = 42;
> intArray[1] = 54;
> intArray[2] = 91;
>
> writefln("The lemgth of the intArray is %d.",
> intArray.length);
> }
> [/code]
>
> It compiles with no error or warning - AFAICT. However, it chokes
> upon execution with:
>
> dnormandin@ ~/programming/dmd2/code
> 01:40 pm >> arrays_in_d
> core.exception.RangeError at arrays_in_d(7): Range violation
>
> I've read and re-read the tutorial content, but it's not jumping out
> at me.
The array initially has length of zero.
You need to write something like:
intArray = new int[3];
or
intArray.length = 3;
before putting anything into it.
More information about the Digitalmars-d-learn
mailing list