Thread overview |
---|
August 19, 2018 Static initialization of static arrays is weird | ||||
---|---|---|---|---|
| ||||
I have a two dimensional static array in a game board struct and want to explicitly set the default value for each cell. Now typing the whole 9x9 array out would be cumbersome and I can't change the default constructor of a struct, so I played around with initializers and found some... strange behavior. Demo: ``` import std.stdio: writeln; struct T { int[3][3] arr = [2, 1]; this(int stub) { arr[0][0] = 9; } } void main() { T.init.writeln; T(0).writeln; } ``` Output: ``` T([[2, 1, 0], [0, 0, 118033674], [723976, 0, 4100]]) T([[9, 2, 2], [1, 1, 1], [0, 0, 0]]) ``` So it seems that in the .init value, it puts [2, 1] in the first array and the rest is garbage. But in the constructor case, it filled the first two static arrays with 2 and 1 and the other one is 0 / garbage (can't tell). I turned to the spec: https://dlang.org/spec/arrays.html#static-init-static But that doesn't really help specify this case. Should I submit a bugzilla issue? I don't know what's supposed to happen here. And in the mean time, what's the easiest way to initialize a (2d) static array with a value? |
August 19, 2018 Re: Static initialization of static arrays is weird | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis | On Sunday, 19 August 2018 at 11:20:41 UTC, Dennis wrote:
> I have a two dimensional static array in a game board struct and want to explicitly set the default value for each cell. Now typing the whole 9x9 array out would be cumbersome and I can't change the default constructor of a struct, so I played around with initializers and found some... strange behavior.
>
> Demo:
> ```
> import std.stdio: writeln;
>
> struct T {
> int[3][3] arr = [2, 1];
> this(int stub) {
> arr[0][0] = 9;
> }
> }
>
> void main() {
> T.init.writeln;
> T(0).writeln;
> }
> ```
> Output:
> ```
> T([[2, 1, 0], [0, 0, 118033674], [723976, 0, 4100]])
> T([[9, 2, 2], [1, 1, 1], [0, 0, 0]])
> ```
> So it seems that in the .init value, it puts [2, 1] in the first array and the rest is garbage. But in the constructor case, it filled the first two static arrays with 2 and 1 and the other one is 0 / garbage (can't tell). I turned to the spec:
>
> https://dlang.org/spec/arrays.html#static-init-static
>
> But that doesn't really help specify this case. Should I submit a bugzilla issue? I don't know what's supposed to happen here.
I think the spec is pretty clear; the elements of the right-hand-side initializer array are interpreted as per-element initializer, i.e., `result[0] = 2, result[1] = 1` (rest: default-init). So (latest) LDC outputs
```
T([[2, 2, 2], [1, 1, 1], [0, 0, 0]])
T([[9, 2, 2], [1, 1, 1], [0, 0, 0]])
```
DMD's T.init featuring garbage is clearly a bug, as is the divergence wrt. T.init and the initialization of the struct literal `T(0)` (which seems to be correct, initializing like LDC, but only starting with v2.072). With a non-literal, `auto t = T(0); t.writeln();`, the result is `[[9, 1, 0], <garbage from T.init>]` again.
So please do file a bugzilla issue.
|
August 19, 2018 Re: Static initialization of static arrays is weird | ||||
---|---|---|---|---|
| ||||
Posted in reply to kinke | On Sunday, 19 August 2018 at 12:10:08 UTC, kinke wrote:
> I think the spec is pretty clear; the elements of the right-hand-side initializer array are interpreted as per-element initializer, i.e., `result[0] = 2, result[1] = 1` (rest: default-init).
I can't find where in the spec it says that the elements of the right-hand-side initializer array are interpreted as per-element initializer, or that the rest will be default initialized. But that would mean that this works:
```
int[3][3] arr = 0;
```
And it does for a local variable, but for a struct member it says (both dmd and ldc):
```
cannot implicitly convert expression 0 of type int to int[3][3]
```
So that should be fixed too.
|
Copyright © 1999-2021 by the D Language Foundation