Thread overview
default initialization of char arrays
1 day ago
Walter Bright
1 day ago
Walter Bright
1 day ago
Walter Bright
1 day ago
pete
1 day ago
Walter Bright
22 hours ago
Dennis
1 day ago
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;
```
1 day ago
https://dlang.org/spec/arrays.html#static-init-static
1 day ago
You can also use this for default initialization of structs:

```
struct S { int a=1,b=2,c=3; char[10] x = 0; }
```
1 day ago

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]);
1 day ago
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.

22 hours ago

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:

OSVERSIONINFOEXW vi = {sizeof(vi),0,0,0,0,{0},0,0,0,VER_NT_WORKSTATION};

Got incorrectly translated to this in druntime:

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