www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - multidimensional dynamic arrays

reply Trass3r <mrmocool gmx.de> writes:
What's the short way to create a multidimensional dynamic array? I 
always forget how to do such stuff due to its strange syntax.

auto array = new ubyte[width*height][numSprites];
doesn't work cause width*height is no "Integer constant expression"
Nov 15 2009
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Trass3r wrote:
 What's the short way to create a multidimensional dynamic array? I
 always forget how to do such stuff due to its strange syntax.
 
 auto array = new ubyte[width*height][numSprites];
 doesn't work cause width*height is no "Integer constant expression"
auto array = new ubyte[][](width*height, numSprites); params might be backwards..
Nov 15 2009
parent reply Trass3r <mrmocool gmx.de> writes:
Ellery Newcomer schrieb:
 
 auto array = new ubyte[][](width*height, numSprites);
 
 params might be backwards..
Ah yeah, thanks, that was it. Don't get the reason for that syntax. Anyway it should really be described in the language docs. I'd expect it to be at http://www.digitalmars.com/d/2.0/arrays.html but "Rectangular Arrays" only explains how to declare them.
Nov 16 2009
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 16 Nov 2009 08:42:28 -0500, Trass3r <mrmocool gmx.de> wrote:

 Ellery Newcomer schrieb:
  auto array = new ubyte[][](width*height, numSprites);
  params might be backwards..
Ah yeah, thanks, that was it. Don't get the reason for that syntax.
Especially now that static arrays are legitimate value types, new ubyte[1][2] looks to the compiler like a dynamic array of 1 ubyte[1]'s. -Steve
Nov 16 2009
parent Trass3r <mrmocool gmx.de> writes:
Steven Schveighoffer schrieb:
 
 Especially now that static arrays are legitimate value types, new 
 ubyte[1][2] looks to the compiler like a dynamic array of 1 ubyte[1]'s.
 
Ah ok. Maybe it's a good idea to alter the error message when the compiler detects a non-constant expression? Like if you meant to create a dynamic array use the []() syntax..
Nov 16 2009
prev sibling parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Trass3r wrote:
 Ellery Newcomer schrieb:
 auto array = new ubyte[][](width*height, numSprites);

 params might be backwards..
Ah yeah, thanks, that was it. Don't get the reason for that syntax.
I think it has something to do with uniformity inside templates or something. alias ubyte[][] Foo; Foo foo = new Foo(1,2); still not sure about params.. I should look this up..
Nov 16 2009