RealDocs

TArray::InsertUninitialized

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

Description

Opens a gap of Count uninitialized element slots at Index, shifting later elements right. The caller must construct each new slot before any further array operation.

Caveats & Gotchas

  • The opened slots contain garbage memory. Unlike Insert(), no constructor is called — all Count slots must be placement-new'd or memcpy'd into before they can be read safely.
  • Inserting into the middle of the array requires shifting all elements after Index, which is an O(N) operation. Prefer appending + sorting, or use a different data structure if mid-array insertions are frequent.
  • Do not use with element types that have non-trivial constructors; use InsertDefaulted() or Insert() instead.

Signature

UE_NODEBUG UE_FORCEINLINE_HINT void InsertUninitialized(SizeType Index, SizeType Count = 1)

Parameters

Name Type Description Default
Index SizeType Position at which to open the gap. Existing elements at and after this index are shifted right.
Count SizeType Number of uninitialized slots to insert. 1

Return Type

void

Example

Reserve a slot in the middle for a placement-new C++
TArray<FMyPOD> Data;
Data.SetNum(5);

// Open a slot at index 2
Data.InsertUninitialized(2);
// Placement-new into the raw slot
::new(&Data[2]) FMyPOD(42);

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.