System.Runtime.InteropServices.Marshal.AllocHGlobal Method

Allocates memory from the unmanaged memory of the process by using the specified number of bytes.

Syntax

[System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.MayFail)]
public static IntPtr AllocHGlobal (int cb)

Parameters

cb
The required number of bytes in memory.

Returns

A pointer to the newly allocated memory. This memory must be released using the Marshal.FreeHGlobal(IntPtr) method.

Remarks

Marshal.AllocHGlobal(IntPtr) is one of two memory allocation methods in the System.Runtime.InteropServices.Marshal class. (Marshal.AllocCoTaskMem(int) is the other.) This method exposes the Win32 tp://go.microsoft.com/fwlink/?LinkId=148628 function from Kernel32.dll.

When Marshal.AllocHGlobal(int) calls LocalAlloc, it passes a LMEM_FIXED flag, which causes the allocated memory to be locked in place. Also, the allocated memory is not zero-filled.

Requirements

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0