Memory leak with dynamic array

bearophile bearophileHUGS at lycos.com
Mon Apr 12 04:41:26 PDT 2010


Something that I have forgotten, for the original poster: the D append is not templated code, so this produces no template bloat as in the C++ <vector>. This is another compromise.

-----------

Ellery Newcomer:

>It won't work for classes, though.<

You have to write:

class List(T) {
    static struct Node {
        T data;
        Node* next;
        this(T d, Node* p=null) {
            data = d;
            next = p;
        }
    }

    Node* lh;

    this(T[] arr) {
        foreach_reverse (el; arr)
            lh = new Node(el, lh);
    }
}

void main() {
    auto items = new List!int([1, 2, 3]);
}



>Ick, that turned into a mess fast.<

I'd like to be a computer scientist, able to create a DMeta, similar to PyMeta. A DMeta can be much cleaner than that "mess".
See the original OMeta:
http://tinlizzie.org/ometa/
and its Python version:
http://washort.twistedmatrix.com/
https://launchpad.net/pymeta

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list