Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
September 30, 2014 Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
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=- |
September 30, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike James Attachments: | 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 = 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. |
September 30, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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:
>
>> 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.
Thanks ketmar,
You'll notice that it's actually a dynamic array of structs containing dynamic arrays - does this change your initializing?
Regards, -=mike=-
|
September 30, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike James | 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 |
October 01, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Tuesday, 30 September 2014 at 17:22:32 UTC, Steven Schveighoffer wrote:
> 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
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=-
|
October 01, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike James | 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=-
|
October 01, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike James Attachments: | 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... > > 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. |
October 01, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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:
>
>> 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.
Thanks ketmar, that did the trick.
Regards, -=mike=-
|
October 01, 2014 Re: Initialising multidimensional dynamic arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike James | 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
|
Copyright © 1999-2021 by the D Language Foundation