Avoid deallocate empty arrays?

Steven Schveighoffer schveiguy at yahoo.com
Thu Dec 17 17:46:59 UTC 2020


On Thursday, 17 December 2020 at 16:11:37 UTC, IGotD- wrote:
> It's common using arrays for buffering, that means constantly 
> adding elements and empty the elements. I have seen that when 
> the number of elements is zero, the array implementation 
> deallocates the array which is shown with capacity is zero. 
> This of course leads to constant allocation and deallocation of 
> the array.
>
> One workaround is just to allocate the array once, then keep 
> track of the position yourself rather than shrinking the array 
> so that the length field always track the number of elements. 
> This is possible but if you want dynamic increasing capacity 
> you have track that yourself too.
>
> However, is there a way to tell the array not deallocate the 
> array and just increasing the array when necessary.

This isn’t correct. Can you post the code that led you to believe 
this?

-Steve


More information about the Digitalmars-d-learn mailing list