NativeMemory.AllocZeroed Method

Definition

Overloads

AllocZeroed(UIntPtr)

Allocates and zeroes a block of memory of the specified size, in bytes.

AllocZeroed(UIntPtr, UIntPtr)

Allocates and zeroes a block of memory of the specified size, in elements.

AllocZeroed(UIntPtr)

Source:
NativeMemory.cs
Source:
NativeMemory.cs
Source:
NativeMemory.cs

Important

This API is not CLS-compliant.

Allocates and zeroes a block of memory of the specified size, in bytes.

C#
[System.CLSCompliant(false)]
public static void* AllocZeroed(nuint byteCount);
C#
[System.CLSCompliant(false)]
public static void* AllocZeroed(UIntPtr byteCount);

Parameters

byteCount

nuint

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

Returns

Void*

A pointer to the allocated and zeroed block of memory.

Attributes

Exceptions

Allocating byteCount of memory 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 calloc API.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

AllocZeroed(UIntPtr, UIntPtr)

Source:
NativeMemory.Unix.cs
Source:
NativeMemory.Unix.cs
Source:
NativeMemory.Unix.cs

Important

This API is not CLS-compliant.

Allocates and zeroes a block of memory of the specified size, in elements.

C#
[System.CLSCompliant(false)]
public static void* AllocZeroed(nuint elementCount, nuint elementSize);
C#
[System.CLSCompliant(false)]
public static void* AllocZeroed(UIntPtr elementCount, UIntPtr elementSize);

Parameters

elementCount

nuint

The count, in elements, of the block to allocate.

elementSize

nuint

The size, in bytes, of each element in the allocation.

Returns

Void*

A pointer to the allocated and zeroed block of memory.

Attributes

Exceptions

Allocating elementCount * elementSize bytes of memory failed.

Remarks

This method allows elementCount and elementSize to be 0. It returns 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 calloc API.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9