Setting array length without initializing/reallocating.

Ola Fosheim Grostad ola.fosheim.grostad at gmail.com
Tue Dec 15 10:20:23 UTC 2020


On Saturday, 12 December 2020 at 00:53:09 UTC, Jonathan Levi 
wrote:
> Wow, there went several hours of debugging.
>
> Increasing the length of a slice, by setting its length, will 
> initialize the new elements and reallocate if necessary.
>
> I did not realize length was "smart", I guess I should have 
> guessed.
>
> Anyway, to work around this, and probably also be more clear, 
> create a new slice from the same pointer.
>
> `array = array.ptr[0..newLength];`

D and Go have both messed up the concept of a view of an array 
and owning the backing store of an array. I suggest using slices 
like c++ spans, only make them smaller, then create you own 
dynamic array ADT wrapper for explicit array ownership of the 
full array.


More information about the Digitalmars-d mailing list