Thread overview
Bidimensional dynamic array problem
Mar 06, 2007
orgoton
Mar 06, 2007
orgoton
Mar 06, 2007
Frits van Bommel
Mar 06, 2007
Frits van Bommel
March 06, 2007
I have this

struct Vertex{
(...)
float z;
}

then:
Vertex vData[][];

Somewhere later:
vData.length=sizeX;
foreach(Vertex strip[]; vData[]) strip.length=sizeY;

Here the array is initialized. So far, I think I haven't done anything illegal. (SizeX and SizeY are runtime values, ushort vars). So first I say how many strips I want, and next, I put in how many elements that strip will have.

ulong m;
foreach (Vertex[] strip; vData)
{
      foreach (Vertex vertex; strip)
      {
           file.read(vertex.height);
           m++;
      }
}

Here I read the values from a file. First, I select a strip, and then go through all the elements of that array and read their values. "m" is a counter to see how many elements where read.

The thing is, m=0 at the end of the loop. What did I do wrong?
March 06, 2007
Just a minor correction:
> struct Vertex{
> (...)
> float z;
> }

should be

struct Vertex{
(...)
float height;
}

(typo)

March 06, 2007
orgoton wrote:
> I have this
> 
> struct Vertex{
> (...)
> float z;
> }
> 
> then:
> Vertex vData[][];
> 
> Somewhere later:
> vData.length=sizeX;
> foreach(Vertex strip[]; vData[]) strip.length=sizeY;

foreach(inout Vertex[] strip; vData) strip.length = sizeY;
or just:
foreach(inout strip; vData) strip.length = sizeY;

Note: only the inout is significant, the rest is just nitpicking :P.
If you don't specify 'inout', you're just modifying a local copy of the element...

And IIRC something like "vData = new Vertex[][](sizeX, sizeY)" should replace that entire code sequence, initializing the outer array to an array of sizeX arrays of length sizeY.

> Here the array is initialized. So far, I think I haven't done anything illegal. (SizeX and SizeY are runtime values, ushort vars). So first I say how many strips I want, and next, I put in how many elements that strip will have.

You haven't done anything illegal, just something wrong ;).
You haven't initialized the array the way you thought you did: it's just an array of _empty_ dynamic arrays at this point.

> ulong m;
> foreach (Vertex[] strip; vData)

(this loop doesn't require 'inout' since the array itself isn't modified, just the data it references)

> {
>       foreach (Vertex vertex; strip)

foreach(inout Vertex vertex; strip)
(or: foreach(inout vertex; strip))

>       {
>            file.read(vertex.height);
>            m++;
>       }
> }
> 
> Here I read the values from a file. First, I select a strip, and then go through all the elements of that array and read their values. "m" is a counter to see how many elements where read.
> 
> The thing is, m=0 at the end of the loop. What did I do wrong?

Since you didn't initialize the elements of vData, their lengths are all still zero. So your inner loop is never executed.
March 06, 2007
Frits van Bommel wrote:
> orgoton wrote:
> 
>> ulong m;
>> foreach (Vertex[] strip; vData)
> 
> (this loop doesn't require 'inout' since the array itself isn't modified, just the data it references)

Sorry, that's not right. Not even the data is modified, just the data referenced *by* the data...

>> {
>>       foreach (Vertex vertex; strip)
> 
> foreach(inout Vertex vertex; strip)
> (or: foreach(inout vertex; strip))
> 
>>       {
>>            file.read(vertex.height);
>>            m++;
>>       }
>> }