NAME
malloc, free, realloc, calloc, cfree, memalign, valloc, mallocmap, mallopt, mallinfo, malloc_debug, malloc_verify, alloca - memory allocator
SYNOPSIS
#include <malloc.h>
char *malloc(size)
unsigned size;
int free(ptr)
char *ptr;
char *realloc(ptr, size)
char *ptr;
unsigned size;
char *calloc(nelem, elsize)
unsigned nelem, elsize;
int cfree(ptr)
char *ptr;
char *memalign(alignment, size)
unsigned alignment;
unsigned size;
char *valloc(size)
unsigned size;
void mallocmap()
int mallopt(cmd, value)
int cmd, value;
struct mallinfo mallinfo()
#include <alloca.h>
char *alloca(size)
int size;
SYSTEM V SYNOPSIS
#include <malloc.h>
void *malloc(size)
size_t size;
void free(ptr)
void *ptr; void *realloc(ptr, size)
void *ptr;
size_t size;
void *calloc(nelem, elsize)
size_t nelem;
size_t elsize;
void *memalign(alignment, size)
size_t alignment;
size_t size;
void *valloc(size)
size_t size;
The XPG2 versions of the functions listed in this section are declared as they are in SYNOPSIS above, except free(), which is declared as:
void free(ptr)
char *ptr;
DESCRIPTION
These routines provide a general-purpose memory allocation package. They maintain a table of free blocks for efficient allocation and coalescing of free storage. When there is no suitable space already free, the allocation routines call sbrk() (see brk(2)) to get more memory from the system.
Each of the allocation routines returns a pointer to space suitably aligned for storage of any type of object. Each returns a NULL pointer if the request cannot be completed (see DIAGNOSTICS).
malloc() returns a pointer to a block of at least size bytes, which is appropriately aligned.
free() releases a previously allocated block. Its argument is a pointer to a block previously allocated by malloc(), calloc(), realloc(), malloc(), or memalign().
realloc() changes the size of the block referenced by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If unable to honor a reallocation request, realloc() leaves its first argument unaltered. For backwards compatibility, realloc() accepts a pointer to a block freed since the most recent call to malloc(), calloc(), realloc(), valloc(), or memalign(). Note: using realloc() with a block freed before the most recent call to malloc(), calloc(), realloc(), valloc(), or memalign() is an error. calloc() uses malloc() to allocate space for an array of nelem elements of size elsize, initializes the space to
valloc(size) is equivalent to memalign(getpagesize(), size).
mallocmap() prints a map of the heap to the standard output. mallocmap() prints each block's address, size (in bytes) and status (free or busy). A block must have a size that is no larger than the current extent of the heap.
mallopt() allows quick allocation of small blocks of memory. mallopt() tells subsequent calls to malloc() to allocate holding blocks containing small blocks. Under this small block algorithm, a request to malloc() for a small block of memory returns a pointer to one of the pre-allocated small
mallinfo() can be used during program development to determine the best settings for the parameters set by mallopt(). Do not call mallinfo() until after a call to malloc(). mallinfo() provides information describing space usage. It returns a mallinfo structure, defined in <malloc.h> as:
struct mallinfo {
malloc(), realloc(), memalign() and valloc() return a nonNULL pointer if size is 0, and calloc() returns a non-NULL pointer if nelem or elsize is 0, but these pointers should not be dereferenced.
Note: Always cast the value returned by malloc(), realloc(), calloc(), memalign(), valloc() or alloca().
SYSTEM V DESCRIPTION
The XPG2 versions of malloc(), realloc(), memalign() and valloc() return NULL if size is 0. The XPG2 version of calloc() returns NULL if nelem or elsize is 0.
RETURN VALUES
On success, malloc(), calloc(), realloc(), memalign(), valloc() and alloca() return a pointer to space suitably aligned for storage of any type of object. On failure, they return NULL.
free() and cfree() return:
mallinfo() returns a struct mallinfo.
SYSTEM V RETURN VALUES
If size is 0, the XPG2 versions of malloc(), realloc(), memalign() and valloc() return NULL.
If nelem or elsize is 0, the XPG2 version of calloc() returns NULL.
free() does not return a value.
ERRORS
malloc(), calloc(), realloc(), valloc(), memalign(), cfree(), and free() will each fail if one or more of the following are true:
The allocation heap is found to have been
SEE ALSO
csh(1), ld(1), brk(2), getrlimit(2), sigvec(2), sigstack(2)
Stephenson, C.J., Fast Fits, in Proceedings of the ACM 9th Symposium on Operating Systems, SIGOPS Operating Systems Review, vol. 17, no. 5, October 1983.
Core Wars, in Scientific American, May 1984.
DIAGNOSTICS
More detailed diagnostics can be made available to programs using malloc(), calloc(), realloc(), valloc(), memalign(), cfree(), and free(), by including a special relocatable object file at link time (see FILES). This file also provides routines for control of error handling and diagnosis, as defined below. Note: these routines are not defined in the standard library.
int malloc_debug(level)
int level;
int malloc_verify()
malloc_debug() sets the level of error diagnosis and reporting during subsequent calls to malloc(), calloc(), realloc(), valloc(), memalign(), cfree(), and free(). The value of level is interpreted as follows:
WARNINGS
alloca() is machine-, compiler-, and most of all, systemdependent. Its use is strongly discouraged. See getrlimit(2), sigvec(2), sigstack(2), csh(1), and ld(1).
NOTES
Because malloc(), realloc(), memalign() and valloc() return a non-NULL pointer if size is 0, and calloc() returns a non-NULL pointer if nelem or elsize is 0, a zero size need not be treated as a special case if it should be passed to these functions unpredictably. Also, the pointer returned by these functions may be passed to subsequent invocations of realloc().
SYSTEM V NOTES
The XPG2 versions of the allocation routines return NULL when passed a zero size (see SYSTEM V DESCRIPTION above).
BUGS
Since realloc() accepts a pointer to a block freed since the last call to malloc(), calloc(), realloc(), valloc(), or memalign(), a degradation of performance results. The semantics of free() should be changed so that the contents of a previously freed block are undefined.