will .length=0 clear memory

Bill Baxter wbaxter at gmail.com
Wed Nov 5 21:35:28 PST 2008


On Thu, Nov 6, 2008 at 2:02 PM, Big Bill <bigdorkyb at tengu.com> wrote:
> i just wonder here,
>
> free(x); will free contents of x, but will x.length=0 free memory too?

No it does not, and this is how you can implement a "reserve_capacity"
kind of function for D arrays.

void reserve_capacity(T)(ref T[] a, size_t N) {
    size_t orig_length = a.length;
    if (orig_length >= N) return;
    a.length = N;
    a.length = orig_length;
}


More information about the Digitalmars-d-learn mailing list