2.10 Memory Allocation

The memory-allocation routines allow you to allocate, free, and reallocate blocks of memory. Memory-allocation routines are declared in the include file MALLOC.H. The C++ _set_new_handler functions allow you to redefine the action of the C++ new operator and are declared in include file NEW.H.

Routine Use

_alloca Allocates a block of memory from the program's stack
_bfreeseg Frees a based heap
_bheapseg Allocates a based heap
calloc, _bcalloc, _fcalloc, _ncalloc Allocate storage for an array
_expand, _bexpand, _fexpand, _nexpand, Expand or shrink a block of memory without moving its location  
free, _bfree, _ffree, _free Free an allocated block
_freect Returns approximate number of items of given size that could be allocated in the near heap
_halloc Allocates storage for huge array
_heapadd, _bheapadd Add memory to a heap
_heapchk, _bheapchk, _fheapchk, _nheapchk, Check a heap for consistency  
_heapmin, _bheapmin, _fheapmin, _nheapmin Release unused memory in a heap
_heapset, _bheapset, _fheapset, _nheapset, Fill free heap entries with a specified value  
_heapwalk, _bheapwalk, _fheapwalk, _nheapwalk, Return information about each entry in a heap  
_hfree Frees a block allocated by _halloc
malloc, _bmalloc, _fmalloc, _nmalloc, Allocate a block of memory  
_memavl Returns approximate number of bytes available for allocation in the near heap
_memmax Returns size of largest contiguous free block in the near heap
_msize, _bmsize, _fmsize, _nmsize Return size of an allocated block
realloc, _brealloc, _frealloc, _nrealloc, Reallocate a block to a new size  
_set_new_handler, _set_bnew_handler, _set_fnew_handler, _set_hnew_handler, _set_nnew_handler Enable an error-handling mechanism
_stackavail Returns size of stack space available for allocation with _alloca

Some memory-management routines, such as malloc, are available in different versions that begin with _b, _f, or _n. These variations are described in the following section.

The malloc and free routines allocate and free memory space, respectively, while a program runs. The malloc routine allocates memory from the “heap,” which is a pool of memory not otherwise used by your program. In tiny-, small-, and medium-model programs, the heap consists of unused memory in your program's default data segment. In compact-, large-, and huge-model programs, it is unused memory outside the default data segment.

The malloc and free routines satisfy the memory-allocation requirements of most programs. More specialized memory-management routines are discussed below.

The realloc and _expand routines can expand or shrink an allocated memory block. They behave differently in cases in which there is not enough room to expand the block in its current location. In this case, realloc moves the block as needed, but _expand does not.

The calloc routine allocates memory for an array and initializes every byte in the allocated block to 0.

The _halloc routine is similar to calloc, except that it can allocate memory for a huge array (one that exceeds 64K in size). This routine is useful when you need a very large data object, or if you need to return allocated memory to the operating system for subsequent calls to the _spawn family of functions.