digitalmars.D - default initialization of char arrays
- Walter Bright (19/19) Sep 08 This came up in an email exchange. Consider:
- Walter Bright (1/1) Sep 08 https://dlang.org/spec/arrays.html#static-init-static
- Walter Bright (4/4) Sep 08 You can also use this for default initialization of structs:
- pete (8/12) Sep 08 I didn't notice you could do that. In the past I have used
- Walter Bright (2/3) Sep 08 It seems to be an overlooked feature, which is why I posted it.
- Dennis (25/27) Sep 09 That is not what the email exchange was about. Last DConf you
- JN (6/10) Sep 10 I feel like in such scenario a warning should be issued, or even
This came up in an email exchange. Consider: ``` import core.stdc.stdio; __gshared char[10] xxx = [0]; // initialize xxx to all zeros void main() { foreach (c; xxx) printf("%d\n", c); } ``` A `char` default initializes to 0xFF. The programmer wanted to default initialize the array of char to 0, and so used [0] to initialize it. This resulted in `[0,255,255,255,255,255,255,255,255,255]`. He asked how to default initialize it to 0 without having to tediously enumerate the 0 for each element in the initializer. The answer is: ``` __gshared char[10] xxx = 0; ```
Sep 08
You can also use this for default initialization of structs: ``` struct S { int a=1,b=2,c=3; char[10] x = 0; } ```
Sep 08
On Monday, 8 September 2025 at 15:50:06 UTC, Walter Bright wrote:You can also use this for default initialization of structs: ``` struct S { int a=1,b=2,c=3; char[10] x = 0; } ```I didn't notice you could do that. In the past I have used something like this monstrous thing to make a static array of zero-initialised floats :) ``` float[N] list = iota(0, N).map!((int i) => 0).staticArray!(float[N]); ```
Sep 08
On 9/8/2025 10:03 AM, pete wrote:I didn't notice you could do that.It seems to be an overlooked feature, which is why I posted it.
Sep 08
On Monday, 8 September 2025 at 15:42:27 UTC, Walter Bright wrote:He asked how to default initialize it to 0 without having to tediously enumerate the 0 for each element in the initializer.That is not what the email exchange was about. Last DConf you claimed (paraphrased): 'Default initializing chars to 255 (and floats to nan) in D prevents bugs and makes the programmer's intent clearer.' I shared an experience of the opposite, where this C code from Mingw: ```C OSVERSIONINFOEXW vi = {sizeof(vi),0,0,0,0,{0},0,0,0,VER_NT_WORKSTATION}; ``` Got incorrectly translated to this in druntime: ```D OSVERSIONINFOEXW osvi = { OSVERSIONINFOEXW.sizeof, 0, 0, 0, 0, [0], 0, 0, 0, VER_NT_WORKSTATION }; ``` https://github.com/mingw-w64/mingw-w64/blob/849a151baf187f32eb57b34c00365cbc7d2353a7/mingw-w64-headers/include/versionhelpers.h#L82C5-L82C77 https://github.com/dlang/dmd/blob/dd2a35af794efb1eb72864f7aafbcd0f551e75ca/druntime/src/core/sys/windows/winver.d#L259 Before finding the C code, it was unclear to me what the intention was of `[0]`: is it meant to initialize to `[0, 255, 255, ...]` or `[0, 0, 0, ...]`? I turns out it was supposed to be the latter, but because of D's non-zero default initialization the compiler did the former. Either way, cases like this will be prevented in the future by https://github.com/dlang/dmd/pull/21821
Sep 09
On Monday, 8 September 2025 at 15:42:27 UTC, Walter Bright wrote:This came up in an email exchange. Consider: ``` import core.stdc.stdio; __gshared char[10] xxx = [0]; // initialize xxx to all zerosI feel like in such scenario a warning should be issued, or even compilation error, "static array initialization expects 10 values, only 1 provided". Sooner or later someone will hit the same issue again and spend hours debugging why the array doesn't get zeroed.
Sep 10