Best way to clear dynamic array for reuse

Miguel L via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 14 02:56:02 PDT 2016


On Thursday, 14 July 2016 at 09:12:50 UTC, Jonathan M Davis wrote:
> So, whether you should be using Appender or assumeSafeAppend or 
> neither depends entirely on what you're doing. However, in 
> general, simply appending to dynamic arrays does not result in 
> many reallocations (just like it doesn't result in a lot of 
> realloctions for std::vector or ArrayList). When reallocations 
> become a problem is when you start slicing a dynamic array so 
> that you have other dynamic arrays which refer to the same 
> memory, and you append to those dynamic arrays, or when you 
> reduce the length of an array and then append to it, because in 
> both of those cases, you're appending to dynamic arrays which 
> do not refer to the last element in their underlying memory 
> block.
>
> Hopefully, that makes things at least somewhat clearer.
>
> - Jonathan M Davis

Thank you Jonathan, that really cleared up a lot of things, I 
read the article. But I still have this doubt: is 
assumeSafeAppend() changing a property of the array as "this 
array is never going to be referenced by any other slice, you can 
append or change its length any time and it is never going to be 
reallocated unless it's out of free space"? or it is more like 
"adjust capacity after last operation" so I should be calling it 
whenever I am adjusting length or before appending?


More information about the Digitalmars-d-learn mailing list