MSDN Documentation

Memory Management

This section details the Windows API functions and structures related to managing system memory, including allocation, protection, and virtual memory operations.

Core Memory Allocation

Allocates a region of pages within the virtual address space of the calling process.
Allocates a region of memory in another process.
Allocates a block of memory from a specified heap.

Memory Deallocation

Deallocates a region of pages within the virtual address space of the calling process.
Deallocates a region of memory in another process.
Deallocates a specified block of memory and returns it to the specified heap.

Memory Information and Control

Retrieves information about a region of pages in the virtual address space of the calling process.
Changes the protection on the region of pages.
Retrieves a handle to the process's default heap.

VirtualAlloc

LPVOID VirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
Allocates a region of pages within the virtual address space of the calling process. The allocated region can be in memory or in the process's virtual address space.

Parameters

lpAddress: LPVOID - The starting address of the region of pages to be allocated. If this parameter is NULL, the system determines where to allocate the region.
dwSize: SIZE_T - The size, in bytes, of the region of pages to allocate.
flAllocationType: DWORD - The type of memory allocation. This parameter can be one or more of the following values:
  • MEM_COMMIT: Allocates physical memory for the specified reserved memory pages.
  • MEM_RESERVE: Reserves a range of the process's virtual address space without any actual physical storage being allocated.
  • MEM_RESET: Indicates that data in the memory range specified by lpAddress and dwSize is no longer of interest.
flProtect: DWORD - The memory protection for the region of pages to be allocated.

Return Value

If the function succeeds, the return value is the base address of the allocated region. If the function fails, the return value is NULL.

VirtualFree

BOOL VirtualFree(LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType);
Deallocates a region of pages within the virtual address space of the calling process.

Parameters

lpAddress: LPVOID - A pointer to the base address of the region of pages to be freed.
dwSize: SIZE_T - The size, in bytes, of the region to be freed. This value must be equal to the value of the dwSize parameter that the lpAddress parameter points to when the region was allocated.
dwFreeType: DWORD - The type of free operation. This parameter can be one of the following values:
  • MEM_DECOMMIT: Decommits the specified region of pages, setting its state to MEM_FREE. A decommitted region must not be processed by the application.
  • MEM_RELEASE: Releases the specified region of pages, setting its state to MEM_FREE. This operation must be performed on a region that was committed or reserved with MEM_RELEASE.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

HeapAlloc

LPVOID HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes);
Allocates a block of memory from the specified heap. The allocated memory is uninitialized.

Parameters

hHeap: HANDLE - A handle to the heap that the memory will be allocated from. This handle is returned by the HeapCreate function. The special heap handle returned by GetProcessHeap can also be used.
dwFlags: DWORD - The allocation type. This parameter can be zero or one of the following values:
  • HEAP_ZERO_MEMORY: The memory allocated by this function is initialized to zero.
  • HEAP_GENERATE_EXCEPTIONS: If the function fails because there is not enough available memory to satisfy the requested allocation, the function calls the current handler for the exception_no_memory exception.
dwBytes: SIZE_T - The number of bytes to be allocated.

Return Value

If the function succeeds, it returns a pointer to the allocated memory block. If the function fails, it returns NULL.

HeapFree

BOOL HeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem);
Deallocates a specified block of memory and returns it to the specified heap.

Parameters

hHeap: HANDLE - A handle to the heap that the memory to be freed belongs to. This handle is returned by the HeapCreate function. The special heap handle returned by GetProcessHeap can also be used.
dwFlags: DWORD - This parameter is reserved and must be zero.
lpMem: LPVOID - A pointer to the memory block to be freed. This pointer is returned by a previous call to the HeapAlloc or HeapReAlloc function.

Return Value

If the function succeeds, it returns TRUE. If the function fails, it returns FALSE.

VirtualQuery

SIZE_T VirtualQuery(LPCVOID lpAddress, PMEMORY_BASIC_INFORMATION lpBuffer, SIZE_T dwLength);
Retrieves detailed information about a region of pages in the virtual address space of the calling process.

Parameters

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 specified memory region.
dwLength: SIZE_T - The size of the buffer pointed to by the lpBuffer parameter, in bytes.

Return Value

If the function succeeds, the return value is the number of bytes stored into the buffer. If the function fails, the return value is zero.

VirtualProtect

BOOL VirtualProtect(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect);
Changes the protection on the specified region of committed pages of virtual memory.

Parameters

lpAddress: LPVOID - A pointer to the base address of the region of pages to be changed.
dwSize: SIZE_T - The size, in bytes, of the region of pages to be changed.
flNewProtect: DWORD - The new protection setting for the region of pages.
lpflOldProtect: PDWORD - A pointer to a variable that receives the previous access protection value for the first page of the specified region.

Return Value

If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

GetProcessHeap

HANDLE GetProcessHeap();
Retrieves a handle to the process's default heap. The heap created by this function is private to the process.

Return Value

If the function succeeds, it returns a handle to the process's default heap. If the function fails, it returns NULL.