RealDocs

TArray::SetNumUnsafeInternal

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

Description

Directly sets the internal element count without touching memory, running destructors, or reallocating. Only valid for shrinking — it simply changes the stored count and is intended for hand-rolled serialization or custom allocator scenarios where the caller manages construction/destruction manually.

Caveats & Gotchas

  • Does NOT call destructors on the elements it logically removes. If the element type owns heap memory or references (TArray, TSharedPtr, FString, etc.), those resources will leak. Only safe for trivially destructible types or when you have manually destructed the removed elements first.
  • Must only shrink — passing a NewNum greater than Num() violates the checkSlow assertion and results in undefined behaviour in shipping builds (where the check is compiled out).

Signature

void SetNumUnsafeInternal(SizeType NewNum)

Parameters

Name Type Description Default
NewNum SizeType The new logical element count. Must be <= current Num() and >= 0.

Return Type

void

Example

Trim a pre-built buffer of trivial types C++
// After manually writing into a pre-allocated TArray<int32>,
// update the count to reflect only the filled portion.
TArray<int32> Output;
Output.Reserve(MaxPossible);
int32 Written = FillData(Output.GetData(), MaxPossible);
Output.SetNumUnsafeInternal(Written); // no destructor calls, just update count

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.