Setting array length without initializing/reallocating.

Jackson22 jack.sonof at gmail.com
Sun Dec 13 20:03:46 UTC 2020


On Sunday, 13 December 2020 at 17:26:45 UTC, rikki cattermole 
wrote:
> On 14/12/2020 6:01 AM, Jackson22 wrote:
>>>>
>>>> `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"?
>
> No bounds checking. That slice can extend into memory that 
> isn't of that type or even allocated to the process.

No *automatic* bounds checking != no bounds checking.

There's a reason .ptr exist, I wish people would stop pretending 
that using it where it is appropriate is somehow going to lead to 
failure when there are more successful programming languages that 
have zero automatic bounds checking.

> By avoiding that "expensive" memory operation, you instead 
> create a silent memory corruption in its place which is far 
> worse.

Why did you quote expensive? Are you implying it isn't expensive? 
Are you saying re-allocating 4 GB of memory every 6 ms isn't 
expensive?


More information about the Digitalmars-d mailing list