Generating struct .init at run time?

Ali Çehreli acehreli at yahoo.com
Thu Jul 2 15:20:23 UTC 2020


On 7/2/20 3:37 AM, kinke wrote:

 > 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? https://issues.dlang.org/show_bug.cgi?id=11331 and your
 > https://issues.dlang.org/show_bug.cgi?id=16956 are still open.

Wow! I didn't remember that one. According to its date, it was written 
when I was working for Weka. Apparently, ldc took care of it for them 
after all.

 > 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.

What about floating point and char types? Their .init values are not all 
zeros in D spec. (I don't think this matters in my case but still.)

 > So you're saying you have a *stack* that can deal with an 800M struct

Sorry, my test code was too simplistic. The actual code constructs these 
objects in dynamic memory for that exact reason.

 > 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]*`).

Exactly.

These structs are code-generated to reflect ROS interface message types. 
Just like in D, arrays have dynamic/static distinction in ROS so I 
blindly translated the types to D without remembering this .init issue.

The following are the options I am considering:

a) Move to ldc

b) As you and IGotD- suggest, define all members with '= void' and 
memset to zero at runtime. (I will decide whether to  take care of char 
and floating point types specially e.g. by setting doubles to NaN; this 
distinction may not be important in our use case.) Luckily, issue 16956 
you mention above does not affect us because these are non-template structs.

c) Again, as you say, define static arrays as dynamic arrays, 
code-generate a default constructor that sets the length to the actual 
static length, which requires some magic as struct default constructor 
cannot be defined for structs.

d) ?

Ali




More information about the Digitalmars-d-learn mailing list