RealDocs

TArray::SetNumUninitialized

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

Description

Resizes the array to exactly NewNum elements without initialising any newly added memory. Use this when you intend to immediately overwrite every new element, avoiding the cost of zeroing or construction.

Caveats & Gotchas

  • New elements contain indeterminate (garbage) memory — reading any field before writing to it is undefined behaviour. This is intentional for performance-critical paths where you overwrite the data immediately after (e.g. bulk-loading from a file).
  • Unlike SetNum, this does NOT call destructors when shrinking is not required — wait, it does call RemoveAt which destructs. However when growing, constructors are absolutely not called, so do NOT mix this with non-trivially-constructible types unless you placement-new into each slot.

Signature

void SetNumUninitialized(SizeType NewNum, EAllowShrinking AllowShrinking = UE::Core::Private::AllowShrinkingByDefault<AllocatorType>())

Parameters

Name Type Description Default
NewNum SizeType The desired number of elements after the call.
AllowShrinking EAllowShrinking Whether to allow the allocator to shrink when the array shrinks. Defaults to the allocator's default behaviour. UE::Core::Private::AllowShrinkingByDefault<AllocatorType>()

Return Type

void

Example

Resize for bulk binary read C++
TArray<float> Heights;
Heights.SetNumUninitialized(Width * Height); // reserve exact space
Ar.Serialize(Heights.GetData(), Heights.Num() * sizeof(float));

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.