digitalmars.D.learn - Initialising multidimensional dynamic arrays
- Mike James (14/14) Sep 30 2014 Hi,
- ketmar via Digitalmars-d-learn (8/9) Sep 30 2014 On Tue, 30 Sep 2014 15:57:57 +0000
- Mike James (6/16) Sep 30 2014 Thanks ketmar,
- Steven Schveighoffer (4/9) Sep 30 2014 That is what his code does.
- Mike James (6/17) Oct 01 2014 Hi Steve,
- Steven Schveighoffer (10/14) Oct 01 2014 Ah, ok.
- Mike James (18/32) Oct 01 2014 I think I've found a way...
- ketmar via Digitalmars-d-learn (9/17) Oct 01 2014 On Wed, 01 Oct 2014 07:45:48 +0000
- Mike James (4/24) Oct 01 2014 Thanks ketmar, that did the trick.
Hi,
How do I initialise a dynamic array of dynamic arrays?
struct MyData {
SysTime stamp;
short[] data;
this(size_t size) {
data = new short[size];
}
}
MyDataArray mda;
how to initialise mda?
mda = new MyDataArray ?
Thanks.
Regards, -=mike=-
Sep 30 2014
On Tue, 30 Sep 2014 15:57:57 +0000 Mike James via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:How do I initialise a dynamic array of dynamic arrays?do you mean something like this: `int[][] a`? if yes, do this: auto a =3D new int[][](42, 69); and you'll get `int[42][69] a`. heh, people again confused by `new Type[amount]` syntax. that is concrete sign that this syntax will live forever.
Sep 30 2014
On Tuesday, 30 September 2014 at 16:07:28 UTC, ketmar via Digitalmars-d-learn wrote:On Tue, 30 Sep 2014 15:57:57 +0000 Mike James via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Thanks ketmar, You'll notice that it's actually a dynamic array of structs containing dynamic arrays - does this change your initializing? Regards, -=mike=-How do I initialise a dynamic array of dynamic arrays?do you mean something like this: `int[][] a`? if yes, do this: auto a = new int[][](42, 69); and you'll get `int[42][69] a`. heh, people again confused by `new Type[amount]` syntax. that is concrete sign that this syntax will live forever.
Sep 30 2014
On 9/30/14 12:40 PM, Mike James wrote:On Tuesday, 30 September 2014 at 16:07:28 UTC, ketmar via Digitalmars-d-learn wrote:...auto a = new int[][](42, 69);You'll notice that it's actually a dynamic array of structs containing dynamic arrays - does this change your initializing?That is what his code does. -Steve
Sep 30 2014
On Tuesday, 30 September 2014 at 17:22:32 UTC, Steven Schveighoffer wrote:On 9/30/14 12:40 PM, Mike James wrote:Hi Steve, It's true that his code initialises an array of arrays - but my array is an array of structs containing a dynamic array. Regards, -=mike=-On Tuesday, 30 September 2014 at 16:07:28 UTC, ketmar via Digitalmars-d-learn wrote:...auto a = new int[][](42, 69);You'll notice that it's actually a dynamic array of structs containing dynamic arrays - does this change your initializing?That is what his code does. -Steve
Oct 01 2014
On 10/1/14 3:13 AM, Mike James wrote:Hi Steve, It's true that his code initialises an array of arrays - but my array is an array of structs containing a dynamic array. Regards, -=mike=-Ah, ok. There is no trivial way to do it. Unlike C++, struct default ctors cannot be overridden. I see your question has been answered, but I would just tweak it a bit: foreach(ref m; mda) m.data = new short[y]; Only reason for that is, setting length will call some specialized function which eventually ends up doing exactly the same thing. The above is clearer and more correct IMO. -Steve
Oct 01 2014
On Tuesday, 30 September 2014 at 15:57:58 UTC, Mike James wrote:
Hi,
How do I initialise a dynamic array of dynamic arrays?
struct MyData {
SysTime stamp;
short[] data;
this(size_t size) {
data = new short[size];
}
}
MyDataArray mda;
how to initialise mda?
mda = new MyDataArray ?
Thanks.
Regards, -=mike=-
I think I've found a way...
struct MyData {
SysTime stamp;
short[] data;
this(size_t size) {
data = new short[size];
}
}
MyDataArray[] mda; <--- sorry, missing the []s in the original
question...
so in the constructor...
this(size_t x, size_t y) {
mda = new MyDataArray[](x);
foreach(n, _; mda) mda[n].data.length = y;
}
Is there a simpler way?
Regards, -=mike=-
Oct 01 2014
On Wed, 01 Oct 2014 07:45:48 +0000
Mike James via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:
so in the constructor...
=20
this(size_t x, size_t y) {
mda =3D new MyDataArray[](x);
foreach(n, _; mda) mda[n].data.length =3D y;
}
=20
Is there a simpler way?
sorry, but no. btw, if MyDataArray is struct, you should do this:
foreach (ref m; mda) m.data.length =3D y;
or even this:
foreach (ref m; mda =3D new MyDataArray[](x)) m.data.length =3D x;
the thing is that without 'ref' you operates on the local copy, not on
the real array element.
Oct 01 2014
On Wednesday, 1 October 2014 at 08:08:06 UTC, ketmar via Digitalmars-d-learn wrote:On Wed, 01 Oct 2014 07:45:48 +0000 Mike James via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:Thanks ketmar, that did the trick. Regards, -=mike=-so in the constructor... this(size_t x, size_t y) { mda = new MyDataArray[](x); foreach(n, _; mda) mda[n].data.length = y; } Is there a simpler way?sorry, but no. btw, if MyDataArray is struct, you should do this: foreach (ref m; mda) m.data.length = y; or even this: foreach (ref m; mda = new MyDataArray[](x)) m.data.length = x; the thing is that without 'ref' you operates on the local copy, not on the real array element.
Oct 01 2014









Steven Schveighoffer <schveiguy yahoo.com> 