digitalmars.D.learn - Why does multidimensional arrays not allocate properly?
- Jot (6/6) Jan 22 2017 auto x = new int[][](n,m);
- rikki cattermole (6/12) Jan 22 2017 It does allocate them fully, you're indexing them wrong.
- Jot (10/27) Jan 22 2017 No, that isn't the reason, it was cause I was going past the end
- albert-j (3/6) Jan 22 2017 It's x[width][height], but because indexing is 0-based, largest
- Nicholas Wilson (19/49) Jan 22 2017 If you want multidimensional array (matrices, tensors) use either
auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a linear matrix and index by i+w*j, but what's the point of having multidimensional matrices in D if they don't allocate them fully?
Jan 22 2017
On 22/01/2017 9:05 PM, Jot wrote:auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a linear matrix and index by i+w*j, but what's the point of having multidimensional matrices in D if they don't allocate them fully?It does allocate them fully, you're indexing them wrong. void main() { auto x = new int[][](1, 2); x[0][1] = 3; }
Jan 22 2017
On Sunday, 22 January 2017 at 08:07:26 UTC, rikki cattermole wrote:On 22/01/2017 9:05 PM, Jot wrote:No, that isn't the reason, it was cause I was going past the end when I added some new code(the [3,6] was suppose to be [3][6]). I tried it before and it was crashing before I added the new code and visualD seems to not be updating variable values properly anymore so I can't really debug ;/ In anycase, what is the correct notation for indexing? x = new int[][](width, height) and x[height][width] or x[width][height]?auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a linear matrix and index by i+w*j, but what's the point of having multidimensional matrices in D if they don't allocate them fully?It does allocate them fully, you're indexing them wrong. void main() { auto x = new int[][](1, 2); x[0][1] = 3; }
Jan 22 2017
In anycase, what is the correct notation for indexing? x = new int[][](width, height) and x[height][width] or x[width][height]?It's x[width][height], but because indexing is 0-based, largest valid indexes are x[width-1][height-1].
Jan 22 2017
On Sunday, 22 January 2017 at 08:18:35 UTC, Jot wrote:On Sunday, 22 January 2017 at 08:07:26 UTC, rikki cattermole wrote:If you want multidimensional array (matrices, tensors) use either std.experimental.ndslice or mir.ndslice (they are (effectively) the same package, one is a dev version of the other). see https://github.com/libmir/On 22/01/2017 9:05 PM, Jot wrote:auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a linear matrix and index by i+w*j, but what's the point of having multidimensional matrices in D if they don't allocate them fully?The trick is to remember that in D int[][] is effectively (int[])[]. As such indexing the outer dimension gives you the inner dimension. so x[height-1][width-1] will give you the "last" element. visually if - is an int [ - - - - - - - - -] [ - - - - 9 - - - -] [ - - - - - - - - -] the first index (i.e index it once and it) gives you the row, index it again to get the value. so that 9 is x[1][4]It does allocate them fully, you're indexing them wrong. void main() { auto x = new int[][](1, 2); x[0][1] = 3; }No, that isn't the reason, it was cause I was going past the end when I added some new code(the [3,6] was suppose to be [3][6]). I tried it before and it was crashing before I added the new code and visualD seems to not be updating variable values properly anymore so I can't really debug ;/ In anycase, what is the correct notation for indexing? x = new int[][](width, height) and x[height][width] or x[width][height]?
Jan 22 2017