digitalmars.D.learn - Creating 1000 instances
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (24/24) Feb 19 2021 We have:
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (5/29) Feb 19 2021 files = new File[]( 1000 );
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (4/12) Feb 19 2021 You can do
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (4/18) Feb 19 2021 oh, now we can remove brackets
- Siemargl (6/9) Feb 19 2021 Is any differences between x and y definitions?
- Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= (5/14) Feb 19 2021 Although I don't usually use the latter, I can say online d
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (34/43) Feb 19 2021 The only part of the documentation I've found that talks about
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 ?
Feb 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.
Feb 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:You can do files[].each!((ref a) => a = new 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.
Feb 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:oh, now we can remove brackets files.each!((ref a) => a = new File);On Friday, 19 February 2021 at 08:04:19 UTC, Виталий Фадеев wrote:You can do files[].each!((ref a) => a = new 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.
Feb 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);
Feb 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: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);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);
Feb 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: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. } -- SimenSince 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);
Feb 19 2021