Setting array length without initializing/reallocating.

Jackson22 jack.sonof at gmail.com
Sun Dec 13 17:01:31 UTC 2020


On Saturday, 12 December 2020 at 14:12:06 UTC, Mike Parker wrote:
> 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?

How is avoiding an expensive potentially memory leaking operation 
"setting yourself up for failure"?


More information about the Digitalmars-d mailing list