Thread overview
Is `void` the correct way to say "do not initialize this variable"?
Oct 02, 2022
ryuukk_
Oct 02, 2022
ryuukk_
Oct 03, 2022
kdevel
Oct 03, 2022
Paul Backus
Oct 06, 2022
kdevel
Oct 02, 2022
drug007
Oct 03, 2022
tsbockman
Oct 03, 2022
drug007
Oct 03, 2022
tsbockman
October 02, 2022

I have tried to look at the documentation and various places on the DMD source, but i couldn't find the answer

MyStruct test = void;

Does this guarantee that the compiler will not initialize it?

Does it work with static arrays of struct too?

The generated code is different than MyStruct test;

What exactly (by exactly i mean is the behavior documented somewhere?) void does?

October 02, 2022

I got the answer thanks to IRC chat: https://dlang.org/spec/declaration.html#void_init

October 03, 2022
On 10/3/22 02:30, ryuukk_ wrote:
> I have tried to look at the documentation and various places on the DMD source, but i couldn't find the answer

https://dlang.org/spec/declaration.html#void_init

> 
> ```D
> MyStruct test = void;
> ```
> 
> Does this guarantee that the compiler will not initialize it?

Yes

> 
> Does it work with static arrays of struct too?
> 

It works but not as someone could expect. In case of
```
Foo[2] arr = void;
```
`arr` value is not defined, it is not an initialized array of uninitialized elements like you want, it is just uninitialized array.

> The generated code is different than ``MyStruct test;``
> 
> What exactly (by exactly i mean is the behavior documented somewhere?) ``void`` does?
> 



October 03, 2022

On Sunday, 2 October 2022 at 23:45:45 UTC, drug007 wrote:

>

It works but not as someone could expect. In case of

Foo[2] arr = void;

arr value is not defined, it is not an initialized array of uninitialized elements like you want, it is just uninitialized array.

This is incorrect. It is not possible to declare an uninitialized static array variable in D; only the elements are affected by = void.

The meta data of a static array like Foo[2] arr (.ptr and .length) is determined statically at compile time and inserted where needed into the generated code. It is not stored in mutable memory the way a dynamic array/slice's meta data is, and does not need to be initialized at run time.

By contrast, it is possible to declare a completely uninitialized dynamic array, or to just leave its elements uninitialized:

    // Meta data is not initialized, and no elements are allocated.
    // This has no static array equivalent:
    int[] arrA = void;

    // Meta data is initialized, and elements are allocated but not initialized.
    // This is the dynamic equivalent of the static:
    // int[2] arr = void;
    int[] arrB = uninitializedArray!(int[])(2);
October 03, 2022

On Sunday, 2 October 2022 at 23:30:16 UTC, ryuukk_ wrote:

>
MyStruct test = void;

Does this guarantee that the compiler will not initialize it?

It's more of a request, than a guarantee. For example, = void may be ignored for the fields of structs and classes:

struct ABC {
    char a = 'a';
    char b = void;
    char c = 'c';
}

void main() @safe {
    import core.lifetime : emplace;
    import std.stdio : write, writeln;

    ABC abc = { a: 'x', b: 'y', c: 'z' };
    emplace(&abc);
    write(`a: '`, abc.a, `', b: '`);
    if(abc.b != 0)
        write(abc.b);
    else
        write(`\0`);
    writeln(`', c: '`, abc.c, `'`);
}

If the = void actually prevented initialization of b, the above would print:

a: 'a', b: 'y', c: 'c'

However, it actually prints this instead on all D compilers with which I tested:

a: 'a', b: '\0', c: 'c'

This is because it is considered needlessly complicated - and, at least for smallish types, possibly actually slower - to support uninitialized gaps when blitting .init values.

>

Does it work with static arrays of struct too?

Yes.

October 03, 2022
On 10/3/22 09:35, tsbockman wrote:
> On Sunday, 2 October 2022 at 23:45:45 UTC, drug007 wrote:
>> It works but not as someone could expect. In case of
>> ```D
>> Foo[2] arr = void;
>> ```
>> `arr` value is not defined, it is not an initialized array of uninitialized elements like you want, it is just uninitialized array.
> 
> This is incorrect. It is not possible to declare an uninitialized static array variable in D; only the elements are affected by `= void`.
> 
> The meta data of a static array like `Foo[2] arr` (`.ptr` and `.length`) is determined statically at compile time and inserted where needed into the generated code. It is not stored in mutable memory the way a dynamic array/slice's meta data is, and does not need to be initialized at run time.
> 

You are right. I used to complex structure (with indirections) for testing and made wrong statement.

> By contrast, it **is** possible to declare a completely uninitialized dynamic array, or to just leave its elements uninitialized:
> ```D
>      // Meta data is not initialized, and no elements are allocated.
>      // This has no static array equivalent:
>      int[] arrA = void;
> 
>      // Meta data is initialized, and elements are allocated but not initialized.
>      // This is the dynamic equivalent of the static:
>      // int[2] arr = void;
>      int[] arrB = uninitializedArray!(int[])(2);
> ```

October 03, 2022
On Sunday, 2 October 2022 at 23:37:26 UTC, ryuukk_ wrote:
> I got the answer thanks to IRC chat: https://dlang.org/spec/declaration.html#void_init

Quote:

   Implementation Defined: If a void initialized variable's value is used
   before it is set, its value is implementation defined.

Shouldn't this read

   Unspecified Value: If a void initialized variable's value is used
   before it is set, its value is unspecified.

?
October 03, 2022
On Monday, 3 October 2022 at 14:37:35 UTC, kdevel wrote:
> On Sunday, 2 October 2022 at 23:37:26 UTC, ryuukk_ wrote:
>> I got the answer thanks to IRC chat: https://dlang.org/spec/declaration.html#void_init
>
> Quote:
>
>    Implementation Defined: If a void initialized variable's value is used
>    before it is set, its value is implementation defined.
>
> Shouldn't this read
>
>    Unspecified Value: If a void initialized variable's value is used
>    before it is set, its value is unspecified.

Yes, it should. Many of the contributors to the D spec are not very well versed in the precise details of these terms, so mistakes like this occasionally slip through.
October 06, 2022
On Monday, 3 October 2022 at 15:56:02 UTC, Paul Backus wrote:
>> Shouldn't this read
>>
>>    Unspecified Value: If a void initialized variable's value is used
>>    before it is set, its value is unspecified.
>
> Yes, it should. Many of the contributors to the D spec are not very well versed in the precise details of these terms, so mistakes like this occasionally slip through.

Issue 23390 - value of void initialized variable is unspecified (and not subject to UB)