Avoid deallocate empty arrays?

IGotD- nise at nise.com
Thu Dec 17 16:11:37 UTC 2020


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.


More information about the Digitalmars-d-learn mailing list