RealDocs

TArray::AddZeroed

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

Description

Appends Count new elements at the end of the array, zero-initialising their memory via FMemory::Memzero. Returns the index of the first added element. There is also a zero-parameter overload that adds a single zeroed element.

Caveats & Gotchas

  • Constructors are NOT called — this is a raw memory zero. Types with non-trivial constructors (FString, TArray, TSharedPtr) will be in an invalid state if you treat them as constructed objects without explicitly constructing them first.
  • The single-element zero-parameter overload (AddZeroed()) is separate from the counted overload. Both return the index to the first new element.

Signature

SizeType AddZeroed(SizeType Count)

Parameters

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

Return Type

SizeType

Example

Zero-initialise a batch of POD structs C++
struct FParticle { FVector Pos; FVector Vel; float Life; };
TArray<FParticle> Particles;
int32 StartIdx = Particles.AddZeroed(64); // 64 zeroed FParticle slots
// now safe to write into Particles[StartIdx] .. Particles[StartIdx+63]

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.