This section provides an overview and detailed reference for the Win32 API functions related to memory management in Windows.
These functions are fundamental for requesting and releasing blocks of memory from the system heap.
Reserves or commits a region of pages in the virtual address space of the calling process.
Releases, decommits, or both a region of pages within the process's virtual address space.
Reserves, commits, or changes the state of a region of pages in the virtual address space of a specified process.
Releases, decommits, or both a region of pages within the virtual address space of a specified process.
Allocates a specified number of bytes from the heap. The allocated memory is not initialized. If the function succeeds, it returns a pointer to the beginning of the allocated memory block.
Frees a specified batch of memory in the heap. If the function succeeds, the return value is nonzero.
Functions for querying memory status, protecting memory regions, and copying memory blocks.
Retrieves detailed information about a range of pages within the virtual address space of the calling process.
Changes the protection of an allocated region of the virtual address space of the calling process.
Copies a specified number of bytes from a location in memory to another location in memory.
Fills the first `n` bytes of the memory area pointed to by `s` with the specified `c` (first converted to an `unsigned char`).
VirtualAlloc
LPVOID VirtualAlloc(
LPVOID lpAddress,
SIZE_T dwSize,
DWORD flAllocationType,
DWORD flProtect
);
| Parameter | Type | Description |
|---|---|---|
lpAddress |
LPVOID |
The starting address of the region to allocate. If this parameter is NULL, the system determines where to allocate the region. |
dwSize |
SIZE_T |
The size of the region to allocate, in bytes. |
flAllocationType |
DWORD |
The type of memory allocation. This parameter can be one or more of the following values:MEM_COMMIT, MEM_RESERVE, MEM_RESET, MEM_RESET_UNDO, MEM_LARGE_PAGES, MEM_PHYSICAL, MEM_TOP_DOWN. |
flProtect |
DWORD |
The memory protection for the region of pages to be allocated. This parameter can be one of the following values:PAGE_EXECUTE, PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_EXECUTE_WRITECOPY, PAGE_NOACCESS, PAGE_READONLY, PAGE_READWRITE, PAGE_WRITECOPY, PAGE_TARGETS_INVALID, PAGE_TARGETS_NO_UPDATE. |
If the function succeeds, it returns a pointer to the allocated region. Otherwise, it returns NULL.
MEM_RESERVE and MEM_COMMIT together, dwSize is rounded up to the next page boundary.
VirtualFree
BOOL VirtualFree(
LPVOID lpAddress,
SIZE_T dwSize,
DWORD dwFreeType
);
| Parameter | Type | Description |
|---|---|---|
lpAddress |
LPVOID |
A pointer to the starting address of the region of pages to be freed. |
dwSize |
SIZE_T |
The size of the region to free, in bytes. This parameter must be 0 if dwFreeType is MEM_RELEASE. |
dwFreeType |
DWORD |
The type of operation to perform on the specified memory region. This parameter can be one of the following values:MEM_DECOMMIT, MEM_RELEASE. |
If the function succeeds, it returns TRUE. Otherwise, it returns FALSE.
HeapAlloc
LPVOID HeapAlloc(
HANDLE hHeap,
DWORD dwFlags,
SIZE_T dwBytes
);
| Parameter | Type | Description |
|---|---|---|
hHeap |
HANDLE |
A handle to the heap that was created by a call to the HeapCreate function. |
dwFlags |
DWORD |
Flags that control the heap allocation.HEAP_GENERATE_EXCEPTIONS, HEAP_NO_SERIALIZE, HEAP_ZERO_MEMORY. |
dwBytes |
SIZE_T |
The number of bytes to allocate from the heap. |
If the function succeeds, it returns a pointer to the allocated memory. If the function fails, it returns NULL.
VirtualQuery
SIZE_T VirtualQuery(
LPCVOID lpAddress,
PMEMORY_BASIC_INFORMATION lpBuffer,
SIZE_T dwLength
);
| Parameter | Type | Description |
|---|---|---|
lpAddress |
LPCVOID |
A pointer to the starting address of the region of pages to be queried. |
lpBuffer |
PMEMORY_BASIC_INFORMATION |
A pointer to a MEMORY_BASIC_INFORMATION structure that receives information about the region of pages. |
dwLength |
SIZE_T |
The size of the buffer pointed to by lpBuffer, in bytes. |
If the function succeeds, the return value is the number of bytes of information written to the buffer. If the function fails, the return value is 0.