Thread overview
Creating 1000 instances
Feb 19, 2021
Ferhat Kurtulmuş
Feb 19, 2021
Ferhat Kurtulmuş
Feb 19, 2021
Ferhat Kurtulmuş
Feb 19, 2021
Siemargl
Feb 19, 2021
Ferhat Kurtulmuş
Feb 19, 2021
Simen Kjærås
February 19, 2021
We have:
    class File
    {
        // WIN32_FIND_DATAW data;
    }

    void fastReadDir()
    {
        File[] files;

        // reserve space, allocating instances
        files = new File[]( 1000 );  // <--- trouble here ?

        // filling instances
        auto file = files.ptr;

        writeln( file.data );    // <--- or trouble here ?

        // ...
    }

Got:
    SegFault

Goal:
    Allocate memory for 1000 instances at once.

Source:
    https://run.dlang.io/is/xfaXcv

Question:
    What is the true, fastest, beauty way to create 1000 instances of the class File ?


February 19, 2021
On Friday, 19 February 2021 at 08:04:19 UTC, Виталий Фадеев wrote:
> We have:
>     class File
>     {
>         // WIN32_FIND_DATAW data;
>     }
>
>     void fastReadDir()
>     {
>         File[] files;
>
>         // reserve space, allocating instances
>         files = new File[]( 1000 );  // <--- trouble here ?
>
>         // filling instances
>         auto file = files.ptr;
>
>         writeln( file.data );    // <--- or trouble here ?
>
>         // ...
>     }
>
> Got:
>     SegFault
>
> Goal:
>     Allocate memory for 1000 instances at once.
>
> Source:
>     https://run.dlang.io/is/xfaXcv
>
> Question:
>     What is the true, fastest, beauty way to create 1000 instances of the class File ?

files = new File[]( 1000 );
files[] = new File(); // add this

Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want.

February 19, 2021
On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote:
> On Friday, 19 February 2021 at 08:04:19 UTC, Виталий Фадеев wrote:
>> [...]
>
> files = new File[]( 1000 );
> files[] = new File(); // add this
>
> Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want.

You can do

files[].each!((ref a) => a = new File);
February 19, 2021
On Friday, 19 February 2021 at 08:41:06 UTC, Ferhat Kurtulmuş wrote:
> On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote:
>> On Friday, 19 February 2021 at 08:04:19 UTC, Виталий Фадеев wrote:
>>> [...]
>>
>> files = new File[]( 1000 );
>> files[] = new File(); // add this
>>
>> Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want.
>
> You can do
>
> files[].each!((ref a) => a = new File);

oh, now we can remove brackets

files.each!((ref a) => a = new File);
February 19, 2021
On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote:

> Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want.

Is any differences between x and y definitions?

MyClass [] x, y;
x = new MyClass[7];

y= new MyClass[](8);

February 19, 2021
On Friday, 19 February 2021 at 10:02:05 UTC, Siemargl wrote:
> On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote:
>
>> Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want.
>
> Is any differences between x and y definitions?
>
> MyClass [] x, y;
> x = new MyClass[7];
>
> y= new MyClass[](8);

Although I don't usually use the latter, I can say online d editor yields the same ASM output for both:

File[] files = new File[10];
File[] files = new File[](10);
February 19, 2021
On Friday, 19 February 2021 at 10:02:05 UTC, Siemargl wrote:
> On Friday, 19 February 2021 at 08:29:36 UTC, Ferhat Kurtulmuş wrote:
>
>> Since classes are reference types all instances of files will be the same reference of "new File()", which you probably don't want.
>
> Is any differences between x and y definitions?
>
> MyClass [] x, y;
> x = new MyClass[7];
>
> y= new MyClass[](8);

The only part of the documentation I've found that talks about this is here:
https://dlang.org/spec/expression.html#new_expressions

The main difference I know of comes with multidimensional arrays:

    auto a = new int[4][4];
    pragma(msg, typeof(a)); // Prints int[4][]
    auto b = new int[][](4,4);
    pragma(msg, typeof(b)); // Prints int[][]

Since the former is a dynamic array of static arrays, the first size parameter cannot be passed at runtime:

    auto n = 4;
    // Error: variable n cannot be read at compile time
    auto c = new int[n][n];

But must be a compiletime constant:

    enum N = 4;
    auto d = new int[N][n];
    pragma(msg, typeof(d)); // Prints int[4][]

The other syntax however, can take runtime values, but does not encode the size in the type:

    auto e = new int[][](n,n);
    pragma(msg, typeof(e)); // Prints int[][]

The other big thing about the []() syntax is it actually initializes the arrays of arrays for you:

    assert(e[0].length == n);

If you were to use new int[][n], you would need to initialize each array of arrays manually:

    auto f = new int[][n];
    assert(f[0].length == 0); // it's empty
    foreach (i; 0..n) {
        f[i] = new int[n]; // Have to do this yourself.
    }

--
  Simen