ALLOCATING MEMORY WITHIN A PROGRAM

Programs often need to dynamically allocate blocks of memory for internal use. Windows programs can allocate memory either from the program's private local heap or from Windows' global heap. Windows includes two sets of memory allocation functions, one set for using the local heap and one for the global heap.

There are certain trade-offs between local and global memory allocations. Local memory allocations are generally faster and require less overhead, but the memory in the local heap is limited to 64 KB less the combined size of the program's initialized static variables, the program's uninitialized static variables, and the stack. Global memory allocations are not limited to 64 KB, either in the size of single blocks or in the total memory you can allocate. However, pointers to global memory blocks are far pointers, which can be awkward to work with in your program. (You cannot pass a far pointer to small-model and medium-model C library functions, for instance.)

This chart summarizes the differences between memory allocations from the local heap and the global heap:

  Local Heap Global Heap

Size of block: Less than 64 KB Any size
Total memory: Less than 64 KB Free global memory
Pointer: Near Far

You'll probably find local heap allocations convenient for small, short-lived blocks of memory and global heap allocations for large, long-lived blocks.

The memory blocks you allocate can be fixed, moveable, or discardable. You specify the attributes you want when you allocate the memory block. When you write polite, well-mannered Windows programs, you'll probably want to use moveable blocks when allocating global memory, but you can use either fixed or moveable blocks for local memory. How do you deal with a moveable memory block in your program? Very carefully.