Setting array length without initializing/reallocating.

Steven Schveighoffer schveiguy at gmail.com
Mon Dec 14 23:55:09 UTC 2020


On 12/14/20 3:53 PM, Jackson22 wrote:
> On Monday, 14 December 2020 at 01:36:02 UTC, Mike Parker wrote:
>> On Sunday, 13 December 2020 at 20:03:46 UTC, Jackson22 wrote:
>>>
>>> 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.
>>>
>>
>> There's no pretending here. What the OP is doing *is* dangerous.
> 
> If someone writes a wrapper around .ptr which checks. It'd be literally 
> no different than the implementation in druntime.
> 
> Like I said, I wish people would stop pretending that using it where it 
> is appropriate is somehow going to lead to failure. Maybe those people 
> just aren't knowledgeable enough to understand, I don't know.

It's possible you have misinterpreted what the OP is asking for.

Maybe the OP misstated what he is looking to do. Without a clarifying 
response from him, it's hard to tell how to respond, which means we have 
to respond with the most pessimistic interpretation of the post possible.

Yes, you can use .ptr to avoid bounds checks, and it's safe if you do it 
correctly. No you shouldn't use .ptr to create array slices that refer 
to memory outside the range that exists (and using .ptr slicing as 
posted in the original can do this). It's as basic as that.

-Steve


More information about the Digitalmars-d mailing list