Lisp like lists and a problem with TANGO fromStringz

Oskar Linde oskar.lindeREM at OVEgmail.com
Sun Mar 2 11:04:32 PST 2008


bearophile wrote:

> [Unrelated question: why aren't D dynamic arrays triples of values? So they can store their actual length and the memory they have available, so they can support  something like the reserve() of C++ STL. Is the GC already storing such third value? But then I haven't found a reliable way to perform a reserve-like operation.]

The gc is storing that third value. Each allocated chunk has a size. If 
an array starts at the beginning of a gc chunk, its capacity is defined 
as the size of that chunk. Here is a way to reserve space for arrays:

// int rather than size_t due to IFTI sensitivity
void reserve(T)(inout T[] arr, int len) {
	if (len > arr.length) {
		auto t = arr.length;
		arr.length = len;
		arr.length = t;
	}
}

-- 
Oskar



More information about the Digitalmars-d mailing list