Thinking about the difference between fixed and 'dynamic' arrays.

DLearner bmqazwsx123 at gmail.com
Tue Nov 29 23:25:46 UTC 2022


On Tuesday, 29 November 2022 at 19:06:20 UTC, rikki cattermole 
wrote:
[...]

Please see the following example:
```
void main() {

    import std.stdio;

    int[] VarArr1, VarArr2;

    VarArr1.length = 6;
    VarArr1[5] = 10;
    VarArr1[4] = 9;
    VarArr1[3] = 8;
    VarArr1[2] = 7;
    VarArr1[1] = 6;
    VarArr1[0] = 5;

    VarArr2 = VarArr1;
    writeln("VarArr1 = ", VarArr1);
    writeln("VarArr2 = ", VarArr2);

    VarArr1[3] = 40;
    writeln("VarArr1 = ", VarArr1);
    writeln("VarArr2 = ", VarArr2);

    return;
}
```

And it's result:
```
VarArr1 = [5, 6, 7, 8, 9, 10]
VarArr2 = [5, 6, 7, 8, 9, 10]
VarArr1 = [5, 6, 7, 40, 9, 10]
VarArr2 = [5, 6, 7, 40, 9, 10]
```
Many languages have fixed-length arrays, D's such construct works 
as someone approaching the language would expect.
Many languages also have variable length arrays, I suggest D's 
'dynamic array' _does not_ operate as expected.
I'm not suggesting that the result contradicts D's definition of 
'dynamic array', nor it's implementation, just that 'dynamic 
array' is not a reasonable description for a construct that 
behaves like VarArr2[3] becoming 40.


More information about the Digitalmars-d-learn mailing list