Thread overview
How to set array length for multidimensional static arrays
Feb 01, 2016
Namal
Feb 01, 2016
Namal
Feb 01, 2016
cym13
Feb 01, 2016
Jonathan M Davis
Feb 01, 2016
Namal
Feb 01, 2016
Daniel Kozak
Feb 01, 2016
Jonathan M Davis
Feb 01, 2016
Namal
Feb 01, 2016
Daniel Kozak
Feb 01, 2016
Jonathan M Davis
February 01, 2016
I understand that I cannot pass a variable to the static array like in C++, and have to use dynamic arrays. But how can I set the length for them without using a loop?
February 01, 2016
On Monday, 1 February 2016 at 07:41:33 UTC, Namal wrote:
> I understand that I cannot pass a variable to the static array like in C++, and have to use dynamic arrays. But how can I set the length for them without using a loop?

I mean std::vector in C++, not array.
February 01, 2016
On Monday, 1 February 2016 at 07:42:56 UTC, Namal wrote:
> On Monday, 1 February 2016 at 07:41:33 UTC, Namal wrote:
>> I understand that I cannot pass a variable to the static array like in C++, and have to use dynamic arrays. But how can I set the length for them without using a loop?
>
> I mean std::vector in C++, not array.

I'm not sure I understand well because I know nothing about C++ types, so I'll assume you mean dynamic array.

Dynamic arrays are sized at initialization, resized when needed (when adding new elements) and can also be explicitely sized to some size:

    auto arr = [1, 2];

    arr ~= 3;
    assert(arr == [1, 2, 3]);
    assert(arr.length == 3);

    arr.length = 5;
    assert(arr == [1, 2, 3, 0, 0]);
    assert(arr.length == 5);

Note that new elements are set to the initial value of array elements (here int.init).

You can read more about this behaviour in the documentation: http://dlang.org/spec/arrays.html
February 01, 2016
On Monday, February 01, 2016 07:42:56 Namal via Digitalmars-d-learn wrote:
> On Monday, 1 February 2016 at 07:41:33 UTC, Namal wrote:
> > I understand that I cannot pass a variable to the static array like in C++, and have to use dynamic arrays. But how can I set the length for them without using a loop?
>
> I mean std::vector in C++, not array.

I'm not sure what you're trying to do exactly. static arrays cannot be resized. They're a fixed size that's known at compile time. And they're nothing like std::vector at all. e.g.

   int[42] arr;

That array has 42 elements and will always have 42 elements. And you can't do something like

    int[x] arr;

unless x is known at compile time (e.g. it's an enum).

Now, if you want something like std::vector, what you're probably looking for is either dynamic arrays or std.container.Array. And dynamic arrays can be allocated pretty much just like you'd do an array in C++ or Java. e.g.

    auto arr = new int[](42);

Or you can set its length directly. e.g.

    arr.length = 42;

If the new length is shorter than the current length, then it would be identical to slicing the array and reassigning it to the original. e.g.

    arr = arr[0 .. 42];

And if the new length is longer, then each of the new elements is set to the init value of the of the element type.

Or you can append elements with ~=, which would be like using push_back. e.g.

    arr ~= 12;

And if you want to reserve a particular capacity for the dynamic array like you might do with std::vector, then just use reserve. e.g.

    arr.reserve(42);

If you haven't yet, you really should read this article:

http://dlang.org/d-array-article.html

It really should help you understand dynamic arrays in D.

However, be warned that its terminology is a bit off. It uses the term dynamic array for the GC-managed block of memory that a typical dynamic array refers to (per the language spec, T[] is a dynamic array regardless of what kind of memory it refers to, and a GC-managed block of memory has no official term). Similarly, it uses the term slice for T[] rather than dynamic array, and while a non-null dynamic array is a slice of memory of some kind, the term slice is used for a lot more in D than just arrays. Still, while its terminology is a bit wrong, the article is very good and really a must read for anyone who wants to understand dynamic arrays in D.

- Jonathan M Davis

February 01, 2016
Sorry guys that I didn't express myself well. I also mixed some stuff up. What I wanted to ask is this, in c++ this is valid

	int x = 3;
	int y = 10;
	int arr [x][y];

x,y are known at the compile time and arr is a static array. I can't do that in D so what is the best way to declare an array of that size?
February 01, 2016
V Mon, 01 Feb 2016 11:15:40 +0000
Namal via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
napsáno:

> Sorry guys that I didn't express myself well. I also mixed some stuff up. What I wanted to ask is this, in c++ this is valid
> 
> 	int x = 3;
> 	int y = 10;
> 	int arr [x][y];
> 
> x,y are known at the compile time and arr is a static array. I can't do that in D so what is the best way to declare an array of that size?

You can do that:
immutable x = 3;
immutable y = 10;
int[y][x] arr;

February 01, 2016
On Monday, February 01, 2016 11:15:40 Namal via Digitalmars-d-learn wrote:
> Sorry guys that I didn't express myself well. I also mixed some stuff up. What I wanted to ask is this, in c++ this is valid
>
>   int x = 3;
>   int y = 10;
>   int arr [x][y];
>
> x,y are known at the compile time and arr is a static array. I can't do that in D so what is the best way to declare an array of that size?

If x and y are known at compile time, then you can declare a static array using them for dimensions. e.g.

    enum x = 3;
    enum y = 10;
    int[y][x] arr;

But x and y must be something that it is evaluated by the compiler at compile time - e.g. an enum or a static variable. A local variable that just so happens to be directly initialized (like in your example) won't work.

If x and y are _not_ known at compile time, then you can't use the to declare a static array. You'll have to use a dynamic array. e.g.

    auto arr = new int[][](x, y);

- Jonathan M Davis

February 01, 2016
On Monday, 1 February 2016 at 12:12:00 UTC, Jonathan M Davis wrote:
> On Monday, February 01, 2016 11:15:40 Namal via Digitalmars-d-learn wrote:
>> Sorry guys that I didn't express myself well. I also mixed some stuff up. What I wanted to ask is this, in c++ this is valid
>>
>>   int x = 3;
>>   int y = 10;
>>   int arr [x][y];
>>
>> x,y are known at the compile time and arr is a static array. I can't do that in D so what is the best way to declare an array of that size?
>
> If x and y are known at compile time, then you can declare a static array using them for dimensions. e.g.
>
>     enum x = 3;
>     enum y = 10;
>     int[y][x] arr;
>
> But x and y must be something that it is evaluated by the compiler at compile time - e.g. an enum or a static variable. A local variable that just so happens to be directly initialized (like in your example) won't work.
>
> If x and y are _not_ known at compile time, then you can't use the to declare a static array. You'll have to use a dynamic array. e.g.
>
>     auto arr = new int[][](x, y);
>
> - Jonathan M Davis

Thanks alot, I didn't know that way with new.
February 01, 2016
V Mon, 01 Feb 2016 12:19:10 +0000
Namal via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
napsáno:

> On Monday, 1 February 2016 at 12:12:00 UTC, Jonathan M Davis wrote:
> > On Monday, February 01, 2016 11:15:40 Namal via Digitalmars-d-learn wrote:
> >> Sorry guys that I didn't express myself well. I also mixed some stuff up. What I wanted to ask is this, in c++ this is valid
> >>
> >>   int x = 3;
> >>   int y = 10;
> >>   int arr [x][y];
> >>
> >> x,y are known at the compile time and arr is a static array. I can't do that in D so what is the best way to declare an array of that size?
> >
> > If x and y are known at compile time, then you can declare a static array using them for dimensions. e.g.
> >
> >     enum x = 3;
> >     enum y = 10;
> >     int[y][x] arr;
> >
> > But x and y must be something that it is evaluated by the compiler at compile time - e.g. an enum or a static variable. A local variable that just so happens to be directly initialized (like in your example) won't work.
> >
> > If x and y are _not_ known at compile time, then you can't use the to declare a static array. You'll have to use a dynamic array. e.g.
> >
> >     auto arr = new int[][](x, y);
> >
> > - Jonathan M Davis
> 
> Thanks alot, I didn't know that way with new.

you can use this too:
auto arr = new int[y][x];

February 01, 2016
On Monday, February 01, 2016 13:22:23 Daniel Kozak via Digitalmars-d-learn wrote:
> V Mon, 01 Feb 2016 12:19:10 +0000
> Namal via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com>
> napsáno:
>
> > On Monday, 1 February 2016 at 12:12:00 UTC, Jonathan M Davis wrote:
> > > On Monday, February 01, 2016 11:15:40 Namal via Digitalmars-d-learn wrote:
> > >> Sorry guys that I didn't express myself well. I also mixed some stuff up. What I wanted to ask is this, in c++ this is valid
> > >>
> > >>   int x = 3;
> > >>   int y = 10;
> > >>   int arr [x][y];
> > >>
> > >> x,y are known at the compile time and arr is a static array. I can't do that in D so what is the best way to declare an array of that size?
> > >
> > > If x and y are known at compile time, then you can declare a static array using them for dimensions. e.g.
> > >
> > >     enum x = 3;
> > >     enum y = 10;
> > >     int[y][x] arr;
> > >
> > > But x and y must be something that it is evaluated by the compiler at compile time - e.g. an enum or a static variable. A local variable that just so happens to be directly initialized (like in your example) won't work.
> > >
> > > If x and y are _not_ known at compile time, then you can't use the to declare a static array. You'll have to use a dynamic array. e.g.
> > >
> > >     auto arr = new int[][](x, y);
> > >
> > > - Jonathan M Davis
> >
> > Thanks alot, I didn't know that way with new.
>
> you can use this too:
> auto arr = new int[y][x];

True, but that's not the same thing. It creates a dynamic array of static arrays instead of a static array of dynamic arrays. e.g.

    auto a = new int[][](5, 10);
    auto b = new int[5][10];
    auto c = new int[5][](10);
    writeln(typeof(a).stringof);
    writeln(typeof(b).stringof);
    writeln(typeof(c).stringof);

prints

    int[][]
    int[5][]
    int[5][]

And in the cases where the inner element is a static array, its length has to be known compile time, whereas the lengths of the dynamic arrays don't.

On a side note, I really wish that putting the size inside of the brackets was illegal for dynamic arrays to make what's going on clearer, but instead, the outer layer can go either in the brackets or in the parens, whereas where the other layers go changes them between dynamic arrays and static arrays.

- Jonathan M Davis