static void arrays under garbage control?
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 26 08:07:02 PST 2015
On 2/25/15 8:15 PM, captaindet wrote:
> if i understand correctly, static arrays are exempt from GC scanning for
> memory pointers
>
> http://dlang.org/garbage.html : "Pointers in D can be broadly divided
> into two categories: Those that point to garbage collected memory, and
> those that do not. Examples of the latter are pointers created by calls
> to C's malloc(), pointers received from C library routines, pointers to
> static data."
>
>
> but there is also a warning for void arrays
>
> http://dlang.org/arrays.html : The garbage collector "will scan void[]
> arrays for pointers, since such an array may have been implicitly
> converted from an array of pointers or an array of elements that contain
> pointers."
>
>
> does this warning only apply to dynamic void[] arrays but not to static
> void[CTconstant] arrays?
>
> (because sometimes the docs also mean static arrays even if just
> "type[]" is written.)
>
> thanks!
>
> ps: this is for 32bit apps
Somewhat missing in this disscusion is:
the GC does not know what an array's type currently is, it only knows
what it was when it was allocated. So for instance:
ubyte[] arr = cast(ubyte[])(new void[100]); // scanned for pointers
void[] arr = new ubyte[100]; // not scanned for pointers.
In fact, the GC has no idea of type at all. It just knows about memory
blocks, and whether those blocks are flagged as having pointers or not
having pointers. The call to new is what tells the GC "hm.. this is a
type that may contain pointers, set that flag!"
Static data I believe is always scanned conservatively because no type
information is stored for it ever, even on allocation (i.e. program
startup).
-Steve
More information about the Digitalmars-d-learn
mailing list