shared array?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Sep 11 14:58:27 PDT 2015


On Friday, 11 September 2015 at 21:48:14 UTC, Prudence wrote:
> Oh really?!?! I thought slicing used the GC? Is this a recent 
> development or always been that way?

Always been that way. A D slice is just a C pointer + length 
packed together. A slice simply increments the pointer and/or 
decrements the length - no allocation needed.

GC can make slices more convenient, since you don't need to think 
about who owns that memory you're slicing to free it, but that's 
true of pointers and references and everything too.

> you've done above) then release and remalloc when I need to 
> append(not efficient but ok in my senario), then it won't use 
> the GC?

yeah.

you might want to GC.addRange it though so the contents are 
scanned anyway...

(btw the garbage collector is actually pretty nice, why are you 
avoiding it anyway?)

> I guess [] doesn't have a capacity field so I'll have to keep 
> track of that. Other wise, it should be pretty simple.

Nope but you can just use realloc which does track some capacity 
for you. (Actually, the built-in ~= operator does that too, just 
with the GC instead of the C function. It sometimes won't 
reallocate because it knows it already has enough capacity. Read 
more here: http://dlang.org/d-array-article.html )




More information about the Digitalmars-d-learn mailing list