Using C Memory Allocation Functions

The start-up code that LINK attaches to C programs running under Windows contains functions for many of the memory allocation functions in Microsoft C 6, such as calloc, malloc, realloc, and free. The routines in the start-up code convert the normal C memory allocation functions into equivalent Windows memory allocation functions. For instance, the function:

malloc (wSize) ;

is translated into:

LocalAlloc (LMEM_FIXED | LMEM_NOCOMPACT, min (1, wSize)) ;

These functions are included in the Windows start-up code not for your benefit, but because several other C functions from the standard library make calls to these C memory allocation functions. These other C functions cannot work properly without using the Windows memory allocation calls. Although it's not intended that you use these functions, you can use them. Be aware, however, that in compact-memory and large-memory models (which you shouldn't be using for Windows programs anyway), the Windows malloc returns a far pointer to your program's automatic data segment—as opposed to the non-Windows malloc, which returns a far pointer to a block of memory outside the automatic data segment. Also be aware that _fmalloc and halloc are translated into GlobalAlloc calls with a flag that is equal to (GMEM_FIXED | GMEM_NODISCARD), and as you know, you should not use fixed global memory blocks. Moreover, the pointer returned from the Windows halloc is not properly aligned for an array of elements that are not multiples of 2, and the memory is not initialized to 0. The point is clear: Unless you feel deprived doing C programming without malloc, use the Windows functions for all memory allocations.