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
Memory Deallocation
Memory Information and Control
VirtualAlloc
LPVOID VirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect);
Parameters
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.
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);
Parameters
dwSize
parameter that the lpAddress
parameter points to when the region was allocated.
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 withMEM_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);
Parameters
HeapCreate
function. The special heap handle returned by GetProcessHeap
can also be used.
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.
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);
Parameters
HeapCreate
function. The special heap handle returned by GetProcessHeap
can also be used.
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);
Parameters
MEMORY_BASIC_INFORMATION
structure that receives information about the specified memory region.
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);
Parameters
Return Value
If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
GetProcessHeap
HANDLE GetProcessHeap();
Return Value
If the function succeeds, it returns a handle to the process's default heap. If the function fails, it returns NULL.