RealDocs

TArray::Empty

function Core Since 4.0
#include "Containers/Array.h"
Access: public

Description

Destructs all elements and sets Num to 0. Reallocates the backing buffer to exactly Slack capacity, releasing excess memory. Use when you want a clean slate and control over post-clear capacity.

Caveats & Gotchas

  • Empty(0) frees the entire allocation — subsequent adds will reallocate. If you intend to refill the array immediately, pass the expected new count as Slack to avoid a reallocation: Empty(ExpectedCount).
  • Unlike Reset(), Empty() always reallocates to match the requested Slack exactly, even if it means growing the buffer. Reset() only reallocates when it needs to grow.
  • Destructors are called on all current elements before any reallocation, so it is safe for elements with non-trivial destructors (FString, TSharedPtr, etc.).

Signature

void Empty(SizeType Slack = 0)

Parameters

Name Type Description Default
Slack SizeType Desired capacity to retain after emptying. Pass 0 to release all memory. 0

Return Type

void

Example

Clear and recycle capacity for a known next batch size C++
TArray<FHitResult> HitResults;

void AMyActor::Tick(float DeltaTime)
{
    // Clear results from last frame, but pre-allocate for this frame's expected hits
    HitResults.Empty(32);
    GetWorld()->LineTraceMultiByChannel(HitResults, ...);
}

Version History

Introduced in: 4.0

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.