"The total size of a static array cannot exceed 16Mb."
Vladimir Panteleev
thecybershadow at gmail.com
Tue Oct 2 06:37:07 PDT 2007
On Tue, 02 Oct 2007 16:18:26 +0300, Christopher Wright <dhasenan at gmail.com> wrote:
> T[][] SquareArray (T) (int x, int y) {
> T[] block = new T[x * y];
> T[][] ret = [];
> ret.length = x;
> for (int i = 0; i < x; i++) {
> ret[i] = block[i * y .. (i + 1) * y];
> }
>
> return ret;
> }
>
> void main () {
> int[][] array = SquareArray!(int)(9000, 9000);
> }
>
> Slightly tedious, and it gives you reference semantics and places the
> array on the heap (but do you really *want* 16MB stack frames? I imagine
> the OS might have something to say about that).
Thanks, however dynamic arrays are unfortunately slow. To access a random element, you need a dereference, a multiplication+addition, another dereference, another multiplication+addition. Janice Caron's workaround, when modified to use a struct placed in the data segment, requires only one dereferencing/multiply+adding - while a static array placed in the data segment requires only arithmetics to access.
Also, I was planning to place the array in the uninitialized data segment (I believe it is called BSS in ELF and object files), not the stack - I believe the default stack size for Windows executables to be 256 kB anyway.
P.S. I believe your function can be reduced to this without any significant changes in behavior:
T[][] ret = new T[][x];
foreach(ref row;ret)
row.length = y;
return ret;
--
Best regards,
Vladimir mailto:thecybershadow at gmail.com
More information about the Digitalmars-d
mailing list