Generating struct .init at run time?

kinke noone at
Thu Jul 2 10:37:27 UTC 2020

On Thursday, 2 July 2020 at 07:51:29 UTC, Ali Çehreli wrote:
> Of course, the solution is to define members with '= void'

Since when? and 
your are still 

For recent LDC versions, the 'solution' is to (statically) 
initialize the array with zeros, as fully zero-initialized 
structs don't feature any explicit .init symbols anymore.

> enum elementCount = 1024 * 1024;
> struct S {
>   double[elementCount] a = void;  // <-- HERE
> }
> void main() {
>     S s;
>     assert(typeid(S).initializer.length == double.sizeof * 
> elementCount);
>     assert(typeid(S).initializer.ptr is null);
> }
> Now the program binary is 800M shorter.

So you're saying you have a *stack* that can deal with an 800M 
struct (assuming you used a different `elementCount` for the 
actual tests)?! Even 8 MB should be too large without extra 
compiler/linker options, as that's the default stack size on 
Linux IIRC (on Windows, 2 MB IIRC).

I don't think a struct should ever be that large, as it can 
probably only live on the heap anyway and only passed around by 
refs. I'd probably use a thin struct instead, containing and 
managing a `double[]` member (or `double[elementCount]*`).

More information about the Digitalmars-d-learn mailing list