Best way to clear dynamic array for reuse

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 13 05:05:12 PDT 2016


On 13/07/2016 11:59 PM, Miguel L wrote:
> I am using a temporary dynamic array inside a loop this way:
> A[] a;
> for(....)
> {
> a=[]; //discard array contents
> ... appends thousand of elements to a
> ... use a for some calculations
> }
>
> I would like to know which would be the best way to clear a contents
> avoiding reallocations, as there seems to be lots of garbage collection
> cycles taking place.
>
> The options would be:
>
> a=[];
> a.length=0;
> a=null;
> ...
> any other?
>
> Can you help me please?

All of those "options" do the same thing, remove all references to that 
data.

There is a couple of options. What I will recommend instead is to start 
using buffers and only expand when you append past the length. You will 
probably want a struct to wrap this up (and disable postblit).

Otherwise if you're lazy just disable GC and reenable after the code 
segment.



More information about the Digitalmars-d-learn mailing list