Setting array length without initializing/reallocating.

Mike Parker aldacron at gmail.com
Tue Dec 15 02:21:26 UTC 2020


On Monday, 14 December 2020 at 20:53:39 UTC, 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.

Of course. I'm not arguing otherwise. I don't see that anyone 
else is either. I'm talking about the specific case raised by the 
OP, where the issue isn't just a lack of automatic bounds 
checking, but the lack of any bounds checking at all.

Bounds checking before resizing has one of two possible outcomes: 
a reallocation, or no resizing occurs. The OP explicitly asked 
how to resize an array *without* reallocation, which implies that 
neither outcome of bounds checking is what he's looking for. So 
yes, arbitrarily slicing a pointer beyond its length in that 
situation is asking for trouble.

I mean, if there were more to the story, e.g., the array is 
backed by a block of malloced memory that's large enough for 
newLength, as manual bounds checking would verify, then the 
question of how to resize without reallocating is a moot one, no?





More information about the Digitalmars-d mailing list