digitalmars.D.learn - create and initialise array
- Alex (12/12) Jun 19 2019 Is there a way of creating and initialising a dynamic array ?
- matheus (11/23) Jun 19 2019 What about:
- KnightMare (22/30) Jun 20 2019 another version:
- mw (7/28) Sep 19 2020 Even with this, user has to write two statement, why we can't
- Mike Parker (6/17) Sep 19 2020 The default init values in D are intended to stand out if you're
- Alex (3/3) Jun 20 2019 Thanks Matheus, thats what i needed.
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
Jun 19 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, AlexWhat 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.
Jun 19 2019
On Thursday, 20 June 2019 at 01:32:04 UTC, matheus wrote: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.import std.stdio; import std.array; void main(){ auto s = uninitializedArray!(float[])(100); s[] = 0.0f; writeln(s[0]); }
Jun 20 2019
On Thursday, 20 June 2019 at 07:57:25 UTC, KnightMare wrote:On Thursday, 20 June 2019 at 01:32:04 UTC, matheus wrote:Even with this, user has to write two statement, why we can't just have: auto s = new float[100](0);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.I have the same question, why float/double are init to NaN, What's the reason for this design decision?
Sep 19 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: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.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, What's the reason for this design decision?
Sep 19 2020
Thanks Matheus, thats what i needed. I added a PR to mention this function in the language documentation about arrays.
Jun 20 2019