www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - default initialization of char arrays

reply Walter Bright <newshound2 digitalmars.com> writes:
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
next sibling parent Walter Bright <newshound2 digitalmars.com> writes:
https://dlang.org/spec/arrays.html#static-init-static
Sep 08
prev sibling next sibling parent reply Walter Bright <newshound2 digitalmars.com> writes:
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
parent reply pete <email email.com> writes:
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
parent Walter Bright <newshound2 digitalmars.com> writes:
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
prev sibling next sibling parent Dennis <dkorpel gmail.com> writes:
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
prev sibling parent JN <666total wp.pl> writes:
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.
Sep 10