RealDocs

TArray::AddDefaulted

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

Description

Appends Count new elements at the end of the array, each default-constructed via DefaultConstructItems. Returns the index of the first added element. Unlike AddZeroed, this properly invokes the element's default constructor.

Caveats & Gotchas

  • For trivially-constructible types (int32, float, plain structs with no constructor), DefaultConstructItems falls back to zeroing, so AddDefaulted and AddZeroed behave identically for those. The difference only matters for types with a non-trivial default constructor.
  • The returned index is only valid until the next operation that might reallocate the array. Do not store it across Add/Insert/Reserve calls unless you also store GetData().

Signature

SizeType AddDefaulted(SizeType Count)

Parameters

Name Type Description Default
Count SizeType The number of default-constructed elements to add. Omit or pass 1 for the single-element overload.

Return Type

SizeType

Example

Grow a TArray of FTimerHandle with proper construction C++
TArray<FTimerHandle> Timers;
int32 FirstIdx = Timers.AddDefaulted(4); // 4 properly-constructed FTimerHandles
for (int32 i = 0; i < 4; ++i)
{
    GetWorldTimerManager().SetTimer(Timers[FirstIdx + i], this, &AMyActor::OnTimer, 1.f, true);
}

Version History

Introduced in: 4.14

Version Status Notes
5.6 stable

Feedback

Was this helpful?

Suggest an edit

Select a field above to begin editing.