length = 0 clears reserve

Jethro via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Apr 11 13:00:48 PDT 2017


On Tuesday, 11 April 2017 at 03:00:29 UTC, Jon Degenhardt wrote:
> On Tuesday, 11 April 2017 at 01:59:57 UTC, Jonathan M Davis 
> wrote:
>> On Tuesday, April 11, 2017 01:42:32 Jethro via 
>> Digitalmars-d-learn wrote:
>>> [...]
>>
>> You can't reuse the memory of a dynamic array by simply 
>> setting its length to 0. If that were allowed, it would risk 
>> allow dynamic arrays to stomp on each others memory (since 
>> there is no guarantee that there are no other dynamic arrays 
>> referring to the same memory). However, if you know that there 
>> are no other dynamic arrays referrin to the same memory, then 
>> you can call assumeSafeAppend on the dynamic array, and then 
>> the runtime will assume that there are no other dynamic arrays 
>> referring to the same memory.
>>
>> [snip]
>
> Another technique that works for many cases is to use an 
> Appender (std.array). Appender supports reserve and clear, the 
> latter setting the length to zero without reallocating. A 
> typical use case is an algorithm doing a series of appends, 
> then setting the length to zero and starts appending again.
>
> --Jon

Appender reports clear? Are you sure?

Seems appender is no different than string, maybe worse? string 
as assumeSafeAppend, reserve and clear(although clear necessarily 
reallocates. They should have a function called empty, which 
resets the length to zero but doesn't reallocate.


More information about the Digitalmars-d-learn mailing list