NativeMemory.AlignedAlloc(UIntPtr, UIntPtr) Method

Definition

Important

This API is not CLS-compliant.

Allocates an aligned block of memory of the specified size and alignment, in bytes.

public:
 static void* AlignedAlloc(UIntPtr byteCount, UIntPtr alignment);
[System.CLSCompliant(false)]
public static void* AlignedAlloc (nuint byteCount, nuint alignment);
[System.CLSCompliant(false)]
public static void* AlignedAlloc (UIntPtr byteCount, UIntPtr alignment);
[<System.CLSCompliant(false)>]
static member AlignedAlloc : unativeint * unativeint -> nativeptr<unit>

Parameters

byteCount
UIntPtr

nuint

unativeint

The size, in bytes, of the block to allocate.

alignment
UIntPtr

nuint

unativeint

The alignment, in bytes, of the block to allocate. This must be a power of 2.

Returns

Void*

A pointer to the allocated aligned block of memory.

Attributes

Exceptions

alignment is not a power of two.

Allocating byteCount of memory with alignment failed.

Remarks

This method allows byteCount to be 0 and will return a valid pointer that should not be dereferenced and that should be passed to free to avoid memory leaks.

This method is a thin wrapper over the C aligned_alloc API or a platform dependent aligned allocation API such as _aligned_malloc on Win32.

This method is not compatible with Free(Void*) or Realloc(Void*, UIntPtr). Instead, call AlignedFree(Void*) or AlignedRealloc(Void*, UIntPtr, UIntPtr).

Applies to