15.1.1 Using the Global Heap

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 is 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 object from the global heap. Applications typically allocate large objects from the global heap; these objects can exceed 64K if the applications require that much contiguous space. Windows provides special services for accessing data past the first 64K segment. For more information about these services, see Chapter 16, “More Memory Management.”

To allocate a global memory object, use the GlobalAlloc function. You specify the size and type (fixed, movable, or discardable); GlobalAlloc returns a handle of the memory object. Before you can use the memory object, you must lock it by using the GlobalLock function, which returns the full 32-bit address of the first byte in the memory object. You can then use this long pointer to access the bytes in the object.

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 the address 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 object. Once a movable or discardable memory object is locked, Windows guarantees that the object will remain fixed in memory until it is unlocked. This means the address remains valid as long as the object remains locked, but this also keeps Windows from making the best use of memory if other allocation requests are made. Cooperative applications unlock memory.

The GlobalAlloc function returns the value NULL if an allocation request fails. You should always check the return value to ensure that it is a valid handle. If you want to, you can determine 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 memory object.

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 object have been discarded.

You can free any global memory you may no longer need by using the GlobalFree function. In general, you should free such memory so that other applications can use the space. You should always free global memory before your application terminates.