Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-06 Thread kdevel via Digitalmars-d-learn

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)


Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-03 Thread Paul Backus via Digitalmars-d-learn

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.


Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-03 Thread kdevel via Digitalmars-d-learn

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.

?


Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-03 Thread drug007 via Digitalmars-d-learn

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);
```




Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-03 Thread tsbockman via Digitalmars-d-learn

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();
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.


Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-03 Thread tsbockman via Digitalmars-d-learn

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);
```


Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-02 Thread drug007 via Digitalmars-d-learn

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?








Re: Is `void` the correct way to say "do not initialize this variable"?

2022-10-02 Thread ryuukk_ via Digitalmars-d-learn
I got the answer thanks to IRC chat: 
https://dlang.org/spec/declaration.html#void_init


Is `void` the correct way to say "do not initialize this variable"?

2022-10-02 Thread ryuukk_ via Digitalmars-d-learn
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?