Uninitialized Arrays and GC

Ali Çehreli acehreli at yahoo.com
Fri Dec 23 14:46:45 PST 2011


On 12/23/2011 12:36 PM, Mr. Anonymous wrote:
 > On 23.12.2011 21:51, bearophile wrote:
 >> The current D GC is not precise, so I think the current DMD+GC scan
 >> this array. Future better compilers/runtimes probably will be able to
 >> avoid it (with a shadow stack the gives precise typing information at
 >> runtime, used by a precise GC).
 >
 > Well, if that's really so, then it's not 100% reliable.
 > e.g. you generate an array of random numbers, and one of them appears to
 > be an address of an allocated array. This array won't free even if not
 > used anymore.

I think it's the other way around: whatever this array seems to be 
referring to will not be freed until this array is freed. These are good 
examples of why uninitialized arrays are not for every application.

There is also the option of allocating the memory directly from the GC:

     int * a = cast(int*)GC.calloc(100, GC.BlkAttr.NO_SCAN);

(Some with GC.malloc()).

Now we can fill that array with any value without the fear of having 
been mistaken for references.

Ali



More information about the Digitalmars-d-learn mailing list