Setting array length to 0 discards reserved allocation?
Andrew Godfrey via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jul 24 16:30:11 PDT 2014
Is this a bug? Or am I misunderstanding
something?
import std.stdio, std.string;
import std.array;
unittest {
int[] a;
a.reserve(10);
a.length++;
int *p = &(a[0]);
a.length++; // Increase size. Shouldn't reallocate.
int *p2 = &(a[0]);
assert(p == p2); // And indeed it didn't.
// So sometime later, we want to reuse the allocation. We
'clear' it:
a.length = 0;
// ... and later start using it...
a.length++;
int *q = &a[0];
assert(p == q, format("p: %s; q: %s", p, q)); // FAILS! (on
dmd 2.065.0) Why? Apparently, setting length to 0 caused it to
free the
// reserved
allocation?
}
More information about the Digitalmars-d
mailing list