RealDocs

TArray::SwapMemory

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

Description

Swaps two elements by directly exchanging their raw memory bytes via the global Swap template. No bounds checks are performed — this is the low-level implementation called by Swap after it validates the indices.

Caveats & Gotchas

  • No bounds checking is performed. Passing out-of-range indices causes undefined behaviour in all build configurations including Debug. Use TArray::Swap if you need the safety checks.
  • Swapping the same index with itself is a no-op thanks to the underlying Swap template, but the call overhead is still incurred. Callers that care about performance should check equality before calling.

Signature

UE_FORCEINLINE_HINT void SwapMemory(SizeType FirstIndexToSwap, SizeType SecondIndexToSwap)

Parameters

Name Type Description Default
FirstIndexToSwap SizeType Index of the first element to swap.
SecondIndexToSwap SizeType Index of the second element to swap.

Return Type

void

Example

Low-level element swap inside a custom sort partition C++
// Only use SwapMemory when indices are guaranteed valid (e.g. inside a manual sort).
if (Array[PivotIdx] > Array[CompareIdx])
{
    Array.SwapMemory(PivotIdx, CompareIdx);
}

See Also

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.