Jump to page: 1 2
Thread overview
default initialization of char arrays
36 minutes ago
Vindex9
21 minutes ago
Vindex9
Sep 08
pete
Sep 09
Dennis
Sep 10
JN
14 hours ago
Ogion
12 hours ago
monkyyy
11 hours ago
not you
10 hours ago
monkyyy
9 hours ago
not you
1 day ago
Max Samukha
September 08
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;
```
September 08
https://dlang.org/spec/arrays.html#static-init-static
September 08
You can also use this for default initialization of structs:

```
struct S { int a=1,b=2,c=3; char[10] x = 0; }
```
September 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]);
September 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.

September 09

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

September 10
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 zeros
>

I 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.
1 day ago

On Monday, 8 September 2025 at 15:42:27 UTC, Walter Bright wrote:

>

The answer is:

__gshared char[10] xxx = 0;

I've just run into this:

enum Type: char[4]
{
    invalid = 0,
}

Error: cannot implicitly convert expression 0 of type int to char[4]

Should work, I guess.

14 hours ago

On Wednesday, 10 September 2025 at 23:35:29 UTC, JN wrote:

>

I 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.

Yep, the syntax should be more explicit. Something like this:

float[10] x = [42, ...];   // [42, NaN, ..., NaN]
float[10] y = [1:42, ...]; // [NaN, 42, NaN, ..., NaN]
12 hours ago

On Wednesday, 1 October 2025 at 18:09:27 UTC, Ogion wrote:

>

On Wednesday, 10 September 2025 at 23:35:29 UTC, JN wrote:

>

I 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.

Yep, the syntax should be more explicit. Something like this:

float[10] x = [42, ...];   // [42, NaN, ..., NaN]
float[10] y = [1:42, ...]; // [NaN, 42, NaN, ..., NaN]

nah they just going to break it without a replacement

« First   ‹ Prev
1 2