All nontrivial C programs allocate memory at runtime. The standard C library provides four memory-management routines:
Mem interface repackages these routines as a set of macros and routines that are less prone to error and that provide a few additional capabilities.
Unfortunately, memory-management bugs are common in C programs, and they are often difficult to diagnose and fix. For example, the fragment
p = malloc(nbytes); ... free(p);
malloc to allocate a block of
nbytes of memory, assigns the address of the first byte of that block to
p and the block it points to, and frees the block. After the call to
p holds a dangling pointer — a pointer that refers to ...