## digitalmars.D.learn - array questions

• yes (11/11) Jan 11 2009 Hello again
• Sergey Gromov (12/24) Jan 11 2009 Um, if that's your code, everything should crash at this point (or throw
• yes (3/35) Jan 11 2009 I meant it to be an dynamic array argument, but that the function wouldn...
• Daniel Keep (11/19) Jan 11 2009 Dynamic arrays do not have a fixed size, thus code needs to check the
• bearophile (8/17) Jan 11 2009 If you are using normal D dynamic arrays you have to loop through the wh...
• yes (2/2) Jan 12 2009 thank you
yes <yes no.com> writes:
```Hello again

is it possible to make a dynamic array less dynamic?

int[][] array;

array[0].length = 10; //has to be set at runtime
writefln(array[1].length); // writes also 10

Because I now have to loop through the whole array to check for correct size.

also, can this be done?

int size;
size = 10; //runtime
void function( int[size][] array){}

thank you
```
Jan 11 2009
Sergey Gromov <snake.scaly gmail.com> writes:
```Sun, 11 Jan 2009 17:17:54 -0500, yes wrote:

Hello again

is it possible to make a dynamic array less dynamic?

int[][] array;

array[0].length = 10; //has to be set at runtime

Um, if that's your code, everything should crash at this point (or throw
in debug mode): array is null, that is, empty, so there is no array[0].

You should array.length = 10; first.  Then you'll get an array of 10
empty arrays of int, and will be able to set their lengths separately:

array[0].length = 10; // OK
assert(array[1].length == 0); // OK

If you want to set up quickly you can write

int[][] array = new int[][](10, 10);

This will give you a sort of square matrix, bit internally it will be an
array of arrays nevertheless.

also, can this be done?

int size;
size = 10; //runtime
void function( int[size][] array){}

No, static arrays are static, i.e. compile time.
```
Jan 11 2009
yes <yes no.com> writes:
```
Hello again

is it possible to make a dynamic array less dynamic?

int[][] array;

array[0].length = 10; //has to be set at runtime

Um, if that's your code, everything should crash at this point (or throw
in debug mode): array is null, that is, empty, so there is no array[0].

You should array.length = 10; first.  Then you'll get an array of 10

well, at least to 2. my fault.
empty arrays of int, and will be able to set their lengths separately:

array[0].length = 10; // OK
assert(array[1].length == 0); // OK

If you want to set up quickly you can write

int[][] array = new int[][](10, 10);

thank you, that will make all the element to be at least a certain size.

This will give you a sort of square matrix, bit internally it will be an
array of arrays nevertheless.

also, can this be done?

int size;
size = 10; //runtime
void function( int[size][] array){}

No, static arrays are static, i.e. compile time.

I meant it to be an dynamic array argument, but that the function wouldn't need
to check the size of all the elements itself.
```
Jan 11 2009
Daniel Keep <daniel.keep.lists gmail.com> writes:
```yes wrote:
[snip]
also, can this be done?

int size;
size = 10; //runtime
void function( int[size][] array){}

No, static arrays are static, i.e. compile time.

I meant it to be an dynamic array argument, but that the function wouldn't
need to check the size of all the elements itself.

Dynamic arrays do not have a fixed size, thus code needs to check the
number of elements when it uses them.

Static arrays have a fixed size, but that size MUST be fixed at compile
time.

You can convert static arrays into dynamic arrays, but not the other way
around (at least, not without copying the contents).

When you compile with the -release flag, it disables range checking.
You can also avoid it by going via pointers, but that's just asking for
trouble.

-- Daniel
```
Jan 11 2009
bearophile <bearophileHUGS lycos.com> writes:
```yes:

is it possible to make a dynamic array less dynamic?
int[][] array;
array[0].length = 10; //has to be set at runtime
writefln(array[1].length); // writes also 10
Because I now have to loop through the whole array to check for correct size.

If you are using normal D dynamic arrays you have to loop through the whole
array to check for correct size.
Otherwise you have to create a new and different data structure, an array that
is guaranteed to be rectangular. Probably there are already such data structure
done by someone else (the downside is that DMD may handle them less
efficiently. The up side is that the resulting memory allocated is probably
contiguous, that leads to better cache coherence, less memory wasted, and
ability to quickly reshape the matrix on the fly).
Built-in dynamic arrays are just one of the many possible kinds of arrays a
programmer may need. I think the current D design is good enough: a very common
and flexible case is built-in, and you can create the other different data
structures by yourself (or you can import them from a lib like Tango).

also, can this be done?
int size;
size = 10; //runtime
void function( int[size][] array){}

The size of a dynamic array is an information known only at runtime, so the D
type system is unable to know it at compile time. So for the D type system is
impossible to perform that control at compile time.
What you ask may be done by a more powerful type system in special situations
(when the compiler can infer at compile time the size), but you need a more
powerful type system.

Bye,
bearophile
```
Jan 11 2009
yes <yes no.com> writes:
```thank you

I think I understand how it works
```
Jan 12 2009