Description
Resizes the array to exactly NewNum elements. If the array grows, the new elements are zero-initialised via FMemory::Memzero rather than being constructed. If it shrinks, the removed elements are properly destructed.
Caveats & Gotchas
- • Zero-initialisation is NOT the same as default-construction. Any element type whose constructor sets non-zero initial state (e.g. FVector is {0,0,0} by coincidence, but a custom struct with a non-trivial constructor may be left in an invalid state). Prefer SetNum or AddDefaulted for types with meaningful constructors.
- • When growing, the new elements skip their constructors entirely — reading a field like a TArray member inside the zeroed element is undefined until it is explicitly initialised.
Signature
void SetNumZeroed(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 removing elements. Defaults to the allocator's default behaviour. | UE::Core::Private::AllowShrinkingByDefault<AllocatorType>() |
Return Type
void Example
Pre-allocate and zero-fill a fixed-size buffer C++
TArray<uint8> Buffer;
Buffer.SetNumZeroed(1024); // 1 KB of zeroed bytes
FMemory::Memcpy(Buffer.GetData(), SourceData, BytesToCopy); Tags
Version History
Introduced in: 4.0
| Version | Status | Notes |
|---|---|---|
| 5.6 | stable | — |
Feedback
Was this helpful?