default initialization of char arrays
Dennis
dkorpel at gmail.com
Tue Sep 9 10:27:20 UTC 2025
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
More information about the Digitalmars-d
mailing list