digitalmars.D.learn - Is `void` the correct way to say "do not initialize this variable"?
- ryuukk_ (10/10) Oct 02 2022 I have tried to look at the documentation and various places on
- ryuukk_ (2/2) Oct 02 2022 I got the answer thanks to IRC chat:
- kdevel (10/12) Oct 03 2022 Quote:
- Paul Backus (4/15) Oct 03 2022 Yes, it should. Many of the contributors to the D spec are not
- kdevel (3/11) Oct 06 2022 Issue 23390 - value of void initialized variable is unspecified
- drug007 (9/25) Oct 02 2022 Yes
- tsbockman (23/30) Oct 02 2022 This is incorrect. It is not possible to declare an uninitialized
- drug007 (3/33) Oct 03 2022 You are right. I used to complex structure (with indirections) for
- tsbockman (36/41) Oct 02 2022 It's more of a request, than a guarantee. For example, `= void`
I have tried to look at the documentation and various places on the DMD source, but i couldn't find the answer ```D 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?
Oct 02 2022
I got the answer thanks to IRC chat: https://dlang.org/spec/declaration.html#void_init
Oct 02 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_initQuote: 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. ?
Oct 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: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.I got the answer thanks to IRC chat: https://dlang.org/spec/declaration.html#void_initQuote: 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.
Oct 03 2022
On Monday, 3 October 2022 at 15:56:02 UTC, Paul Backus wrote:Issue 23390 - value of void initialized variable is unspecified (and not subject to UB)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.
Oct 06 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 answerhttps://dlang.org/spec/declaration.html#void_init```D MyStruct test = void; ``` Does this guarantee that the compiler will not initialize it?YesDoes 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?
Oct 02 2022
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. 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); ```
Oct 02 2022
On 10/3/22 09:35, tsbockman wrote:On Sunday, 2 October 2022 at 23:45:45 UTC, drug007 wrote:You are right. I used to complex structure (with indirections) for testing and made wrong statement.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.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); ```
Oct 03 2022
On Sunday, 2 October 2022 at 23:30:16 UTC, ryuukk_ wrote:```D 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 `struct`s and `class`es: ```D 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.
Oct 02 2022