Creating 1000 instances
Ferhat Kurtulmuş
aferust at gmail.com
Fri Feb 19 08:29:36 UTC 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.
More information about the Digitalmars-d-learn
mailing list