Bidimensional dynamic array problem

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Tue Mar 6 07:50:13 PST 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.


More information about the Digitalmars-d-learn mailing list