TArray allocator and memory pooling

Some strange answers here. Just use



TArray::Reset(ElementCount)


Nukes existing elements and reallocates (if needed) to support the provided number of elements. TArray has both ‘Num’ and ‘Max’ - it doesn’t change the allocation (aka, the slow part) if you are calling Add() on an array that already has enough space allocated. Use it all the time.

Pretty rare that you need to create a custom Allocator outside of the ones which are already provided:

TDefaultAllocator<> - Allocates on-demand as needed.
TInlineAllocator<> - Allocates an initial block then adds as needed.
TFixedAllocator<> - Fixed allocation size, will assert and crash if you try to add more than the specified number of elements.

The number of elements needs to be known at compile time if you are using TInline/TFixed. Otherwise, just use Reserve() or Reset().

1 Like