Marshal.AllocHGlobal Method

Definition

Allocates memory from the unmanaged memory of the process.

Overloads

AllocHGlobal(Int32)

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

AllocHGlobal(IntPtr)

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

AllocHGlobal(Int32)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

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

C#
[System.Security.SecurityCritical]
public static IntPtr AllocHGlobal(int cb);
C#
public static IntPtr AllocHGlobal(int cb);

Parameters

cb
Int32

The required number of bytes in memory.

Returns

IntPtr

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

Attributes

Exceptions

There is insufficient memory to satisfy the request.

Examples

The following example demonstrates calling the AllocHGlobal method. This code example is part of a larger example provided for the Marshal class.

C#
// Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal.AllocHGlobal(100);
Marshal.FreeHGlobal(hglobal);

Remarks

Important

This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. When targeting .NET 6 or later, use the NativeMemory class on all platforms to allocate native memory. When targeting .NET 6 or earlier, use AllocCoTaskMem on all platforms to allocate native memory.

AllocHGlobal is one of two memory allocation methods in the Marshal class. (Marshal.AllocCoTaskMem is the other.) This method exposes the Win32 LocalAlloc function from Kernel32.dll.

When AllocHGlobal 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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

AllocHGlobal(IntPtr)

Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs
Source:
Marshal.Unix.cs

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

C#
[System.Security.SecurityCritical]
public static IntPtr AllocHGlobal(IntPtr cb);
C#
public static IntPtr AllocHGlobal(IntPtr cb);

Parameters

cb
IntPtr

The required number of bytes in memory.

Returns

IntPtr

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

Attributes

Exceptions

There is insufficient memory to satisfy the request.

Remarks

Important

This native memory allocator is a legacy API that should be used exclusively when called for by specific Win32 APIs on the Windows platform. When targeting .NET 6 or later, use the NativeMemory class on all platforms to allocate native memory. When targeting .NET 6 or earlier, use AllocCoTaskMem on all platforms to allocate native memory.

AllocHGlobal is one of two memory allocation methods in the Marshal class. (Marshal.AllocCoTaskMem is the other.) This method exposes the Win32 LocalAlloc function from Kernel32.dll.

When AllocHGlobal 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.

For example code, see Marshal and AllocHGlobal.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0