How do I pass multidimensional static arrays to functions expecting dynamic arrays?

Jonathan M Davis jmdavisProg at gmx.com
Mon Aug 29 19:01:49 PDT 2011


On Tuesday, August 30, 2011 03:43:39 Andrej Mitrovic wrote:
> Right, but I was just trying to temporarily avoid GC allocation so
> I've used a static array instead of a dynamic ones. Also, I don't know
> of another term that is used to describe a int[][] array, other than
> multidimensional.

It _is_ multi-dimensional. I'm not quite sure why Timon is saying that it 
isn't. Ultimately though, a multi-dimensional dynamic array is an array of 
arrays (or array of arrays of arrays of ...). When slicing it, you get the a 
portion of the outermost array. If you index that, you can get at the inner 
arrays to slice those if you want to, but the inner arrays know nothing about 
the outer arrays, and actually taking a "slice" of the whole where you get the 
outer array and some portion of the inner arrays would require creating a new 
array, so you can't really do it.

I can't really think of a clean way of explaining it without diagrams, and 
even then it's a bit of a pain, but with a slice, it's only a matter of 
adjusting its ptr and length properties. If you want a multi-dimensional 
slice, you'd need to adjust the ptr and length properties of the arrays that 
the slice contained, and you can't do that without affecting the original 
arrays unless you copy them. So, ultimately, you need to construct a new 
multi-dimensional array with the pieces that you want if you want a multi-
dimensional slice.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list