Memory allocation
Methods CoTaskMemAlloc GlobalAlloc HeapAlloc LocalAlloc malloc new VirtualAlloc
Comparison I HeapAlloc – can be instructed to raise an exception if memory could not be allocated LocalAlloc – supports allocation of handles which permit the underlying memory to be moved by reallocation without changing the handle value GlobalAlloc and LocalAlloc are wrappers for HeapAlloc HeapFree, GlobalFree, LocalFree – for releasing memory * GlobalAlloc, LocalAlloc and HeapAlloc functions allocates memory from the same heap. There was a difference in memory handling with 16-bit Windows ** Bold means not available for other functions *** Starting with 32-bit Windows GlobalAlloc and LocalAlloc are implemented as wrapper functions that call HeapAlloc
Comparison II VirtualAlloc – allows to specify additional options for memory allocation Can result in higher memory usage malloc – run-time dependent new – compiler and language dependant CoTaskMemAlloc – has advantage of working well in C, C++, VisualBasic The only way to share memory in a COM-based application CoTaskMemFree The virtual memory functions enable a process to manipulate or determine the status of pages in its virtual address space. This allocation use a page granularity Disadvantage to use malloc and new
CoTaskMemAlloc (Combaseapi.h) LPVOID CoTaskMemAlloc(SIZE_T cb); LPVOID CoTaskMemRealloc(LPVOID pv, SIZE_T cb); void CoTaskMemFree( _Frees_ptr_opt_ LPVOID pv); Allocates a block of task memory in the same way that IMalloc::Alloc does IMalloc – interface to manage memory (from Objidl.h) Alloc – allocates block of memory
Heap* (I) (Heapapi.h) HANDLE HeapCreate( DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize ); DECLSPEC_ALLOCATOR LPVOID HeapAlloc( HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes ); Each process has a default heap provided by the system. Applications that make frequent allocations from the heap can improve performance by using private heaps. The HeapCreate function creates a private heap object from which the calling process can allocate memory blocks by using the HeapAlloc function. Memory requested by HeapCreate may or may not be contiguous. Memory allocated within a heap by HeapAlloc is contiguous.
Heap* (II) DECLSPEC_ALLOCATOR LPVOID HeapReAlloc( HANDLE hHeap, DWORD dwFlags, _Frees_ptr_opt_ LPVOID lpMem, SIZE_T dwBytes );
Heap* (III) BOOL HeapFree( HANDLE hHeap, DWORD dwFlags, _Frees_ptr_opt_ LPVOID lpMem ); BOOL HeapDestroy( HANDLE hHeap );
GlobalAlloc _Frees_ptr_opt_ HGLOBAL hMem DECLSPEC_ALLOCATOR HGLOBAL GlobalAlloc( UINT uFlags, SIZE_T dwBytes ); HGLOBAL GlobalFree( _Frees_ptr_opt_ HGLOBAL hMem
LocalAlloc _Frees_ptr_opt_ HLOCAL hMem DECLSPEC_ALLOCATOR HLOCAL LocalAlloc( UINT uFlags, SIZE_T uBytes ); HLOCAL LocalFree( _Frees_ptr_opt_ HLOCAL hMem
VirtualAlloc LPVOID lpAddress, LPVOID VirtualAlloc( SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect ); BOOL VirtualFree( DWORD dwFreeType Reserves, commits, or changes the state of a region of pages in the virtual address space of the calling process. Memory allocated by this function is automatically initialized to zero.
VirtualAlloc2 HANDLE Process, PVOID VirtualAlloc2( PVOID BaseAddress, SIZE_T Size, ULONG AllocationType, ULONG PageProtection, MEM_EXTENDED_PARAMETER *ExtendedParameters, ULONG ParameterCount ); Reserves, commits, or changes the state of a region of memory within the virtual address space of a specified process ParameterCount - the number of extended parameters pointed to by ExtendedParameters.
ExtendedParameters typedef struct MEM_EXTENDED_PARAMETER { struct { DWORD64 Type : MEM_EXTENDED_PARAMETER_TYPE_BITS; DWORD64 Reserved : 64 - MEM_EXTENDED_PARAMETER_TYPE_BITS; } DUMMYSTRUCTNAME; union { DWORD64 ULong64; PVOID Pointer; SIZE_T Size; HANDLE Handle; DWORD ULong; } DUMMYUNIONNAME; } MEM_EXTENDED_PARAMETER, *PMEM_EXTENDED_PARAMETER; A pointer to one or more extended parameters of type MEM_EXTENDED_PARAMETER. You need to provide Type for this sort of parameter
Long pointer (LP*) to … LPTSTR name = NULL; DWORD dwAcctName = 1; can be an integer name = (LPTSTR)GlobalAlloc( GMEM_FIXED, dwAcctName );