why does clearing an array set its capacity to 0?

Vlad Levenfeld via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 1 05:33:13 PDT 2014


I'm trying to implement some wrappers atop preallocated arrays, 
so I'm calling reserve in the constructor, have an invariant to 
ensure that the array.ptr never moves, and use in-contracts to 
make sure any insertion and appends don't put me over the array 
capacity.

I find that I'm able to safely add and remove elements (I 
increment xor decrement the length when I do this) but when I 
call clear on the backing array or set its length to 0, my 
capacity also goes to 0.

Why is this, and what do I do about it?


---
As an aside, I remember seeing a Dconf2013 talk given by Don 
Clugston where he had mentioned that at Sociomantic Labs they 
operate within slices of larger, pinned arrays or something to 
minimize GC activity. (I'm very interested in seeing his 
Dconf2014 talk but can't seem to find a video).

This is basically what I'm trying to do, specifically in the 
context of a custom allocator: allocating custom sliced range 
types that can do fast appends (as well as containers that don't 
call new). If anyone has any advice on this in general I would be 
grateful for it.


More information about the Digitalmars-d-learn mailing list