Thread overview
create and initialise array
Jun 20, 2019
Alex
Jun 20, 2019
matheus
Jun 20, 2019
KnightMare
Sep 19, 2020
mw
Sep 20, 2020
Mike Parker
Jun 20, 2019
Alex
June 20, 2019
Is there a way of creating and initialising a dynamic array ?

for example I am doing this:

auto arr = new float[<big number>];
arr[] = 0.0f;

Profiling indicates that the compiler (gdc) is spending significant time memsetting the whole array to something (nan ?) before I immediately memset it to 0.0f.

It would be good if there was a way of either void initialing it so that the first memset is avoided or a way of replacing the init value with a different one.

Thanks,
Alex
June 20, 2019
On Thursday, 20 June 2019 at 01:06:09 UTC, Alex wrote:
> Is there a way of creating and initialising a dynamic array ?
>
> for example I am doing this:
>
> auto arr = new float[<big number>];
> arr[] = 0.0f;
>
> Profiling indicates that the compiler (gdc) is spending significant time memsetting the whole array to something (nan ?) before I immediately memset it to 0.0f.
>
> It would be good if there was a way of either void initialing it so that the first memset is avoided or a way of replacing the init value with a different one.
>
> Thanks,
> Alex

What about:

//DMD64 D Compiler 2.072.2

import std.stdio;
import std.array;

void main(){
    auto s = uninitializedArray!(float[])(100);
    s[] = 0.0f;
    writeln(s[0]);
}

Matheus.
June 20, 2019
On Thursday, 20 June 2019 at 01:32:04 UTC, matheus wrote:
>>
> import std.stdio;
> import std.array;
>
> void main(){
>     auto s = uninitializedArray!(float[])(100);
>     s[] = 0.0f;
>     writeln(s[0]);
> }
>

another version:
auto arr = new double[ 10 ];
writeln( arr[5] ); // NaN
arr.length += 10;
writeln( arr[15] ); // NaN

imo NaN is useless, weird and unusual coz integrals and pointers are "all bits zeroes" but float and chars are "all bits ones". WTF? its strange that bool.init is false in such case.
.init = "all zeroes" can be faster initialize any block of memory.
for example array of structs coz u dont need copy struct.init to each element and just fill memory with AVX2 zeroed register (or another fastest hack).
with "all zeroes" u can continue work without reinitialization first as arr[15] += 3.14;
probably should be added option to compiler. and again module behavior will be different due to this option. NaN/#FF was worst decision. The road to hell is paved with good intentions.
or do a poll for the last decision for several months and leave it as it is forever or recompile new versions with zeros.
and of course there must be the possibility of increasing the length of the array with a given value.
June 20, 2019
Thanks Matheus, thats what i needed.
I added a PR to mention this function in the language documentation about arrays.

September 19, 2020
On Thursday, 20 June 2019 at 07:57:25 UTC, KnightMare wrote:
> On Thursday, 20 June 2019 at 01:32:04 UTC, matheus wrote:
>>>
>> import std.stdio;
>> import std.array;
>>
>> void main(){
>>     auto s = uninitializedArray!(float[])(100);
>>     s[] = 0.0f;
>>     writeln(s[0]);
>> }

Even with this, user has to write two statement, why we can't just have:

  auto s = new float[100](0);


> another version:
> auto arr = new double[ 10 ];
> writeln( arr[5] ); // NaN
> arr.length += 10;
> writeln( arr[15] ); // NaN
>
> imo NaN is useless, weird and unusual coz integrals and pointers are "all bits zeroes" but float and chars are "all bits ones". WTF? its strange that bool.init is false in such case.
> .init = "all zeroes" can be faster initialize any block of memory.


I have the same question, why float/double are init to NaN, instead of 0? as other post-C++ language does? e.g Java, C# ...

What's the reason for this design decision?

September 20, 2020
On Saturday, 19 September 2020 at 21:53:34 UTC, mw wrote:
> On Thursday, 20 June 2019 at 07:57:25 UTC, KnightMare wrote:

>>
>> imo NaN is useless, weird and unusual coz integrals and pointers are "all bits zeroes" but float and chars are "all bits ones". WTF? its strange that bool.init is false in such case.
>> .init = "all zeroes" can be faster initialize any block of memory.
>
>
> I have the same question, why float/double are init to NaN, instead of 0? as other post-C++ language does? e.g Java, C# ...
>
> What's the reason for this design decision?

The default init values in D are intended to stand out if you're looking at a printf dump or a debugger. NaN for float double, and invalid UTF values for char/wchar/dchar were intentionally chosen. For the integrals, there are no invalid values, so we're stuck with 0.