Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 28, 2014 multidimensional array | ||||
---|---|---|---|---|
| ||||
I'm trying to make a multidimensional array. I feel I've tried every thing. Is there a good guide explaining it? struct Spot { bool dot; } spots = new Spot[][](800,600); assert(spots[800-1][600-1].dot, "Out of bounds"); |
September 28, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel | On Sunday, 28 September 2014 at 04:24:25 UTC, Joel wrote: > I'm trying to make a multidimensional array. I feel I've tried every thing. Is there a good guide explaining it? > > struct Spot { bool dot; } > spots = new Spot[][](800,600); > assert(spots[800-1][600-1].dot, "Out of bounds"); dot is initialized to false, then the assertion fails. But your assertion message leads to think that there is a bound error, which is not the case, the assertion fails because you're expecting dot to be true: ---- import std.stdio; void main(string args[]) { struct Spot { bool dot; } auto spots = new Spot[][](800,600); writeln(spots); assert(!spots[800-1][600-1].dot, "Out of bounds"); } ---- passes without failure. Actually the array size is OK. |
September 28, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
Posted in reply to JKPdouble | On Sunday, 28 September 2014 at 04:38:56 UTC, JKPdouble wrote: > On Sunday, 28 September 2014 at 04:24:25 UTC, Joel wrote: >> I'm trying to make a multidimensional array. I feel I've tried every thing. Is there a good guide explaining it? >> >> struct Spot { bool dot; } >> spots = new Spot[][](800,600); >> assert(spots[800-1][600-1].dot, "Out of bounds"); > > dot is initialized to false, then the assertion fails. But your assertion message leads to think that there is a bound error, which is not the case, the > assertion fails because you're expecting dot to be true: > > ---- > import std.stdio; > > void main(string args[]) > { > struct Spot { bool dot; } > auto spots = new Spot[][](800,600); > > writeln(spots); > assert(!spots[800-1][600-1].dot, "Out of bounds"); > } > ---- > > passes without failure. Actually the array size is OK. I meant: ---- void main(string args[]) { struct Spot { bool dot; } auto spots = new Spot[][](800,600); assert(!spots[800-1][600-1].dot, "element dot is true"); } ---- |
September 28, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel Attachments: | On Sun, 28 Sep 2014 04:24:19 +0000 Joel via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > struct Spot { bool dot; } > spots = new Spot[][](800,600); btw, does anybody know why i can do `new ubyte[256];` but not `new ubyte[256][256];`? hate that. |
September 28, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel | On Sunday, 28 September 2014 at 04:24:25 UTC, Joel wrote: > I'm trying to make a multidimensional array. I feel I've tried every thing. Is there a good guide explaining it? > > struct Spot { bool dot; } > spots = new Spot[][](800,600); > assert(spots[800-1][600-1].dot, "Out of bounds"); You could also take a look at unstd.multidimarray (not my work, but I'm using it extensively at the moment)[1]. [1] http://denis-sh.bitbucket.org/unstandard/unstd.multidimarray.html |
September 28, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
On Sun, Sep 28, 2014 at 10:48:45AM +0300, ketmar via Digitalmars-d-learn wrote: > On Sun, 28 Sep 2014 04:24:19 +0000 > Joel via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > > > struct Spot { bool dot; } > > spots = new Spot[][](800,600); > btw, does anybody know why i can do `new ubyte[256];` but not `new ubyte[256][256];`? hate that. File a bug. T -- I see that you JS got Bach. |
September 28, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On 28/09/2014 08:48, ketmar via Digitalmars-d-learn wrote: > On Sun, 28 Sep 2014 04:24:19 +0000 > Joel via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > >> struct Spot { bool dot; } >> spots = new Spot[][](800,600); > btw, does anybody know why i can do `new ubyte[256];` but not > `new ubyte[256][256];`? hate that. You can do `new ubyte[256][256]`, if the destination type is a ubyte[256][]. The reason is that you are performing an allocation of the form `new T[n]`, which means allocate an array of n instances of type T. In this case, T is ubyte[256], which is a static array type. Stewart. -- My email address is valid but not my primary mailbox and not checked regularly. Please keep replies on the 'group where everybody may benefit. |
September 29, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
Attachments: | On Sun, 28 Sep 2014 07:40:23 -0700 "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn@puremagic.com> wrote: > File a bug. https://issues.dlang.org/show_bug.cgi?id=13556 |
September 29, 2014 Re: multidimensional array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon Attachments: | On Sun, 28 Sep 2014 22:33:40 +0100 Stewart Gordon via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > You can do `new ubyte[256][256]`, if the destination type is a ubyte[256][]. The reason is that you are performing an allocation of the form `new T[n]`, which means allocate an array of n instances of type T. In this case, T is ubyte[256], which is a static array type. it's completely counterintuitive. either `new ubyte[256];` should be disallowed, or `new ubyte[256][256];` should work as i expect it to work. it's the same thing as with const methods: `const A foo ()`. yes, 'const' is a method attribute here, but it's counterintuitive. |
Copyright © 1999-2021 by the D Language Foundation