Setting array length without initializing/reallocating.

Mike Parker aldacron at gmail.com
Sat Dec 12 14:12:06 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];`

You're setting yourself up for failure with that. What are you 
trying to "work around"? The allocation, or the initialization?


More information about the Digitalmars-d mailing list