Zapping a dynamic string

Cecil Ward cecil at cecilward.com
Tue Jul 4 19:23:15 UTC 2023


On Tuesday, 4 July 2023 at 17:46:22 UTC, Steven Schveighoffer 
wrote:
> On 7/4/23 1:01 PM, Cecil Ward wrote:
>> I have a mutable dynamic array of dchar, grown by successively 
>> appending more and more. When I wish to zap it and hand the 
>> contents to the GC to be cleaned up, what should I do? What 
>> happens if I set the .length to zero?
>
> If you want to forget it so the GC can clean it up, set it to 
> `null`. If you set the length to 0, the array reference is 
> still pointing at it.
>
> If you want to reuse it (and are sure that no other things are 
> referring to it), you can do:
>
> ```d
> arr.length = 0;
> arr.assumeSafeAppend;
> ```
>
> Now, appending to the array will reuse the already-allocated 
> buffer space. Obviously, if you have data in there that is 
> still used, you don't want to use this option.
>
> -Steve

Many many thanks for that tip, Steve!


More information about the Digitalmars-d-learn mailing list