Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
November 19, 2012 declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
What is the proper way to declare a multidimensional array? In Java, I can do: double[][] a = new double[M][N]; D does not allow this because of two reasons. 1) M & N are variables and cannot be read at compile time even if they are explicitly initialized. Error: variable N cannot be read at compile time (sawalks) 2) double[17][17] is a static array and cannot be used to initialize a dynamic array. Error: cannot implicitly convert expression (new double[17LU][](17LU)) of type double[17LU][] to double[][] (sawalks) Assigning a length and then trying to access the values obviously will not work: double[][] a; a.length = M * N; a[i][j] = 17; // core.exception.RangeError@sawalks(19): Range violation So what is the proper way to do this in D? Thanks |
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro[17] | Tyro[17]:
> What is the proper way to declare a multidimensional array?
> In Java, I can do:
>
> double[][] a = new double[M][N];
In D (hopefully I have not swapped the to sizes, it's a too much common mistake in my D code):
auto a = new double[][](M, N);
Bye,
bearophile
|
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > auto a = new double[][](M, N);
That's a way to declare and initialize a dynamic array of dynamic arrays. If you want to allocate something else, you need a different syntax.
Example, this creates something rather different:
auto b = new double[][3][](M, N);
In D there are both dynamic arrays and fixed-sized arrays, thankfully.
Bye,
bearophile
|
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > auto b = new double[][3][](M, N);
That doesn't compile, I don't know why. The D array syntax allocation syntax is a bit of a mess. So to do that you have to write:
auto b = new double[][3][](N);
Or:
auto b = new double[][3][N];
And then fill the arrays with a loop. Why is no one complaining about this array allocation syntax?
Bye,
bearophile
|
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > Why is no one complaining about this array allocation syntax?
A syntax I like is:
new double[][3][](N, M)
Where:
- The numbers inside the [] must be always compile-time constants, and they always refer to fixed-sized arrays.
- The numbers inside the () are run-time values and must be used for dynamic arrays. (They can be up to the number of empty []. No more. But maybe they are allowed to be less).
This means this syntax becomes an error:
int n = 5;
auto a = new int[n];
You must write:
int n = 5;
auto a = new int[](n);
This becomes allowed and allocates a fixed-sized array on the heap?
auto a = new int[5];
Bye,
bearophile
|
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 11/18/12 7:53 PM, bearophile wrote:
> Tyro[17]:
>
>> What is the proper way to declare a multidimensional array?
>> In Java, I can do:
>>
>> double[][] a = new double[M][N];
>
> In D (hopefully I have not swapped the to sizes, it's a too much common
> mistake in my D code):
>
> auto a = new double[][](M, N);
>
> Bye,
> bearophile
Thanks... This solved my issue. Much appreciated.
|
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 11/18/12 8:14 PM, bearophile wrote: >> Why is no one complaining about this array allocation syntax? > > A syntax I like is: > > new double[][3][](N, M) Why not simply allow new double[N][3][M]; ? Seems a lot more intuitive/succinct than a separate []() implementation. > Where: > - The numbers inside the [] must be always compile-time constants, and > they always refer to fixed-sized arrays. > - The numbers inside the () are run-time values and must be used for > dynamic arrays. (They can be up to the number of empty []. No more. But > maybe they are allowed to be less). > > This means this syntax becomes an error: > > int n = 5; > auto a = new int[n]; > > You must write: > > int n = 5; > auto a = new int[](n); > > This becomes allowed and allocates a fixed-sized array on the heap? > > auto a = new int[5]; > > Bye, > bearophile |
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro[17] | On Sunday, November 18, 2012 21:06:58 Tyro[17] wrote:
> On 11/18/12 8:14 PM, bearophile wrote:
> >> Why is no one complaining about this array allocation syntax?
> >
> > A syntax I like is:
> >
> > new double[][3][](N, M)
>
> Why not simply allow new double[N][3][M]; ?
> Seems a lot more intuitive/succinct than a separate []() implementation.
Because that declares a dynamic array of static arrays. You'd end up with a dynamic array of length N containing static arrays of length 3 containing static arrays of length M. The fact that you can declare dynamic arrays of static arrays screws with the syntax for declaring multidimensional arrays pretty thoroughly.
- Jonathan M Davis
|
November 19, 2012 Re: declaration/initialization of multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tyro[17] | Tyro[17]:
> Why not simply allow new double[N][3][M]; ?
> Seems a lot more intuitive/succinct than a separate []() implementation.
I think with that there is some ambiguity regarding what coordinates are fixed-sized arrays and what dynamic arrays.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation