www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - noob Q: array out-of-range

reply Duke Normandin <dukeofperl ml1.net> writes:
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 arrays_in_d(7): Range violation

I've read and re-read the tutorial content, but it's not jumping out
at me.
-- 
duke
May 28 2010
next sibling parent reply Don <nospam nospam.com> writes:
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 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.
May 28 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Don:
 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.

In some languages (Perl? Maybe Lua and PHP) arrays auto-create empty slots as needed, but in many languages you need to tell the language that you want some empty slots before you can put values inside them (this stricter behaviour can probably catch some bugs, and allows to produce faster programs). Another way to add items to a D dynamic array like intArray is to append them at the end, the language runtime takes care of creating new slots as needed: int[] intArray; intArray ~= 1; intArray ~= 2; intArray ~= 3; Bye, bearophile
May 28 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Duke Normandin:

 This is the site that I'm using to learn D. If you scroll down 3-4
 screens full, you'll come to the "array" topic. Is this tutorial
 outdated, wrong, or what, because it doesn't deem to sync with what
 you two fine fellows are telling me about creating dynamic arrays in
 the D-Language. TIA..

That part of that tutorial is not outdated, as far as I know it's just wrong. You can auto-create slots in associative arrays: int[int] aa; aa[0] = 1; aa[1] = 1; aa[2] = 1; Lua language uses associative arrays instead of arrays :-) Bye, bearophile
May 28 2010
prev sibling next sibling parent Duke Normandin <dukeofperl ml1.net> writes:
On Fri, 28 May 2010, bearophile wrote:

 Don:
 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.

In some languages (Perl? Maybe Lua and PHP) arrays auto-create empty slots as needed, but in many languages you need to tell the language that you want some empty slots before you can put values inside them (this stricter behaviour can probably catch some bugs, and allows to produce faster programs). Another way to add items to a D dynamic array like intArray is to append them at the end, the language runtime takes care of creating new slots as needed: int[] intArray; intArray ~= 1; intArray ~= 2; intArray ~= 3; Bye, bearophile

Don bearophile Would you guys visit this URL real quick http://compsci.ca/v3/viewtopic.php?t=9518 This is the site that I'm using to learn D. If you scroll down 3-4 screens full, you'll come to the "array" topic. Is this tutorial outdated, wrong, or what, because it doesn't deem to sync with what you two fine fellows are telling me about creating dynamic arrays in the D-Language. TIA.. -- duke
May 28 2010
prev sibling next sibling parent Duke Normandin <dukeofperl ml1.net> writes:
On Fri, 28 May 2010, bearophile wrote:

 Duke Normandin:

 This is the site that I'm using to learn D. If you scroll down 3-4
 screens full, you'll come to the "array" topic. Is this tutorial
 outdated, wrong, or what, because it doesn't deem to sync with what
 you two fine fellows are telling me about creating dynamic arrays in
 the D-Language. TIA..

That part of that tutorial is not outdated, as far as I know it's just wrong. You can auto-create slots in associative arrays: int[int] aa; aa[0] = 1; aa[1] = 1; aa[2] = 1; Lua language uses associative arrays instead of arrays :-) Bye, bearophile

So these two paragraphs in the tutorial are flat out wrong? [quote] You should note that I never specified how long the array should be. Instead I simply assigned values to various positions in the array and it just worked. This is an example of a dynamic array. It will grow to whatever size is required. I can determine the size of an array at any time by accessing the array's length property. The length property can also have a value assigned to it to resize the array. [/quote] Maybe the link for this tutorial at: http://www.prowiki.org/wiki4d/wiki.cgi?FirstLanguage should be removed? -- duke
May 28 2010
prev sibling next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Duke Normandin <dukeofperl ml1.net> wrote:

 So these two paragraphs in the tutorial are flat out wrong?

Absolutely. -- Simen
May 28 2010
parent BCS <none anon.com> writes:
Hello Simen,

 Duke Normandin <dukeofperl ml1.net> wrote:
 
 So these two paragraphs in the tutorial are flat out wrong?
 


Any idea who owns it so it can get changed? For that matter, who added the link? Being paranoid for the moment (it can be fun sometimes :) that blurb is so wrong that I wonder if it's an attempt to frustrate newbies away from D? -- ... <IXOYE><
May 28 2010
prev sibling next sibling parent Duke Normandin <dukeofperl ml1.net> writes:
On Sat, 29 May 2010, Simen kjaeraas wrote:

 Duke Normandin <dukeofperl ml1.net> wrote:

 So these two paragraphs in the tutorial are flat out wrong?

Absolutely.

Then I'm done with _that_ site - can't trust it! Any chance that you could suggest a good beginner's D tutorial? -- duke
May 28 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 28 May 2010 22:42:32 -0400, Duke Normandin <dukeofperl ml1.net>  
wrote:

 On Sat, 29 May 2010, Simen kjaeraas wrote:

 Duke Normandin <dukeofperl ml1.net> wrote:

 So these two paragraphs in the tutorial are flat out wrong?

Absolutely.

Then I'm done with _that_ site - can't trust it! Any chance that you could suggest a good beginner's D tutorial?

It's quite possible that the example code *worked* at some very early version of the D compiler. Nowadays, all arrays are initialized with null as the pointer, guaranteeing that the first time you access array[0], it will segfault. So with the current compilers, that code is *guaranteed* to not work. I speak not from experience, ever since I've started using D it always initializes arrays to null. I just can't imagine that someone would have written such a tutorial without having some experience with D, and without ever having compiled the code. So my logical conclusion is that either 1) at some point, that code did work, 2) the author was so convinced of his D mastery that he didn't bother to try any of his examples, or 3) the page is a blatant attempt to make users not like D. I find 2 unlikely since he specifically mentions how you don't have to initialize arrays. I wonder if someone recalls the feature set of the dmd compiler from July, 2005. -Steve
May 28 2010
prev sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Steven Schveighoffer <schveiguy yahoo.com> wrote:

 I wonder if someone recalls the feature set of the dmd compiler from  
 July, 2005.

That would be http://ftp.digitalmars.com/dmd.128.zip Tested the example given with that compiler, and got ArrayBoundsError. Just as expected. -- Simen
May 29 2010