Best way to clear dynamic array for reuse

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 13 05:06:43 PDT 2016


On Wednesday, 13 July 2016 at 11:59:18 UTC, Miguel L wrote:
> The options would be:
>
> a=[];
> a.length=0;
> a=null;
> ...
> any other?

it really depends of your other code. if you don't have any 
slices of the array, for example, you can use `a.length = 0; 
a.assumeSafeAppend;` -- this will reuse the allocated memory. but 
you should be REALLY sure that you have no array slices are 
floating around! 'cause you effectively promised the runtime that.

otherwise, `a = [];` and `a = null;` is the same, as `[]` is a 
"null array".

most of the time it is ok to use `a = null;` and let GC do it's 
work. it is safe, and you'd better stick to that.


More information about the Digitalmars-d-learn mailing list