Why `Resize()` do not prevent reallocation of TArray?

I have MyComponent, which is publicly inherits from UActorComponent.
MyComponent has property:

UPROPERTY(BlueprintReadOnly, ReplicatedUsing = OnRep_Slots, Category = Stuff)
TArray<FSlotStruct> Slots;

I clears Slots:


Then I call


Then I adds elements to Slots in loop by calling:


And I encountered, that at some point of adding new elements an reallocating of TArray hapens. Number of added elements is equal to SlotsCount, used as argument in Reserve call.

Finaly, to avoid this trouble, I was forced to init cleared TArray with necessary number of default constructed elements, and then rewrite them.

But I want to figure out, what could be the cause of reallocating after reserving.
Please, help me to find th answer.

In such cases it is good to research source code to see when and what it does

Add calls Emplace


Emplace calls AddUninitiazed to create space:

AddUninitiazed reallocates when NumArray (array size varable) with number of added elements is bigger then MaxArray

And MaxArray is set on Reserve

So it should run you predicted, i’m not sure what MoveTemp does but if has to do with anything then use Emplace directly instead of Add.

Also check if you count index right, remeber that size (Num) will be 1 higher them maximum index.

Also note that Reserve argument expects size of entire array, not extra size you want to reserve