[fpc-pascal] Pre-allocated TFPGList or similar
Michael Van Canneyt
michael at freepascal.org
Sun Nov 24 12:14:35 CET 2019
On Sun, 24 Nov 2019, Sven Barth via fpc-pascal wrote:
> Am 24.11.2019 um 10:14 schrieb Michael Van Canneyt:
>>
>>
>> On Sun, 24 Nov 2019, Sven Barth via fpc-pascal wrote:
>>
>>> Am 23.11.2019 um 23:42 schrieb Ryan Joseph via fpc-pascal:
>>>> I need a pre-grown list which I can put (not insert!) items into at
>>>> indexes
>>> without getting "List index out of bounds" errors. For example I want
>>> to start with a list that has 10 empty indexes:
>>>>
>>>> list := TList.Create(10); // 10 empty slots!
>>>>
>>>> list[5] := someItem;
>>>>
>>>> Is this possible using any list type in the RTL? Actually I'd rather
>>>> just
>>> disable all the out of bounds errors becauseĀ I need some performant
>>> which isn't making more checks than need be. I want to use a
>>> heap-based list because the it may need to grow later.
>>> What you're looking for is the Count property. Setting it is
>>> supported by the untyped lists in Classes, the generic ones in FGL as
>>> well as those in Generics.Collections.
>>
>> Better yet, use Capacity.
>>
>> Count is meant to set the number of slots actually used.
>> Capacity is the number of allocated slots.
> But the setter for the items checks against Count, not Capacity, so the
> code I showed will fail if used with Capacity instead of Count. At least
> both properties should be set to work correctly.
You are correct.
I meant to indicate that it is the capacity property that controls the
pre-allocation.
Michael.
More information about the fpc-pascal
mailing list