The global heap contains all of system memory. Windows allocates the memory it needs for code and data from the global heap when it first starts. Any remaining free memory in the global heap is available to applications and Windows libraries.
Applications typically use the global heap for large memory allocations (greater than a kilobyte or so). Although you can allocate larger memory objects from the global heap than you can from the local heap, there is a tradeoff: because it's easier to manipulate local data than it is to manipulate global data, your application will be easier to write if you use only local data.
You can allocate any size of memory block from the global heap. Applications typically allocate large blocks from the global heap; these blocks can exceed 64K if the applications need that much contiguous space. (Windows provides advanced services not discussed in this chapter for accessing data past the first 64K segment.)
To allocate a block of global memory, use the GlobalAlloc function. You specify the size and type (fixed, movable, or discardable); GlobalAlloc returns a handle to the memory block. Before you can use the memory block, you must lock it by using the GlobalLock function, which returns the full 32-bit address of the first byte in the memory block. You can then use this long pointer to access the bytes in the block.
In the following example, the GlobalAlloc function allocates 4096 bytes of movable memory and the GlobalLock function locks it so that the first 256 bytes can be set to 0xFF:
HANDLE hMem;
LPSTR lpMem;
int i;
if ((hMem = GlobalAlloc(GMEM_MOVEABLE, 4096)) != NULL) {
if ((lpMem = GlobalLock(hMem)) != (LPSTR) NULL) {
for (i = 0; i < 256; i++)
lpMem[i] = 0xFF;
GlobalUnlock(hMem);
}
}
In this example, the application unlocks the memory handle by using the GlobalUnlock function immediately after accessing the memory block. Once a movable or discardable memory block is locked, Windows guarantees that the block will remain fixed in memory until it is unlocked. This means the address remains valid as long as the block remains locked, but this also keeps Windows from making the best use of memory if other allocation requests are made. Cooperative applications unlock memory.
Summary: Always check the return value when allocating memory.
The GlobalAlloc function returns NULL if an allocation request fails. You should always check the return value to ensure that it is a valid handle. If desired, you can check to see how much memory is available in the global heap by using the GlobalCompact function. This function returns the number of bytes in the largest contiguous free block of memory.
You should also check the address returned by the GlobalLock function. This function returns a null pointer if the memory handle was not valid or if the contents of the memory block have been discarded.
You can free any global memory you may no longer need by using the GlobalFree function. In general, you should free memory as soon as you no longer need it so that other applications can use the space. You should always free global memory before your application terminates.