Static Constructors

bearophile bearophileHUGS at lycos.com
Sat Oct 4 12:49:00 PDT 2008


Steven Schveighoffer:
> If you want the memory removed as soon as possible, you should zero out the 
> last element, otherwise, the GC will still think the element is being 
> pointed to:
> data[n] = data[$-1];
> data[$-1] = null;
> data.length = data.length - 1;

Really?? Is that another bug of dynamic arrays? I have assumed that dynamic arrays do such things automatically.
I do that in my collections, for example:

...
void length(int newlen) {
    if (newlen < 0 || newlen > this._length)
        throw new ArgumentException("ArrayBuilder.length(newlen):"
                                    " newlen < 0 || newlen > ArrayBuilder.length");
    static if (IsReferenceType!(T)) {
        static if (IsStaticArray!(T)) {
            T Tinit;
            this.data[newlen .. this._length][] = Tinit;
        } else {
            this.data[newlen .. this._length] = T.init;
        }
    }
    this._length = newlen;
}
...

If what you say is true, then it deserves a space on bugzilla and to be fixed soon.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list