malloc(3C) malloc(3C)
NAME
malloc, free, realloc, calloc, memalign, valloc - memory allocator
SYNOPSIS
#include <stdlib.h>
void *malloc(sizet size);
void free(void *ptr);
void *realloc(void *ptr, sizet size);
void *calloc(sizet nelem, sizet elsize);
void *memalign(sizet alignment, sizet size);
void *valloc(sizet size);
DESCRIPTION
malloc() and free() provide a simple memory allocation package.
malloc() returns a pointer to a block of at least size bytes suitably
aligned for any use. The space is not initialized.
The argument to free() is a pointer to a block previously allocated by
malloc(), calloc(), realloc() or valloc(). After free() is performed
this space is made available for further allocation. If ptr is a NULL
pointer, no action occurs. If ptr points to free space, behavior is
undefined.
Undefined results will occur if the space assigned by malloc() is
overrun or if some random number is handed to free().
realloc() changes the size of the block pointed to by ptr to size
bytes and returns a pointer to the (possibly moved) block. The con-
tents will be unchanged up to the lesser of the new and old sizes. If
ptr is NULL, realloc() behaves like malloc() for the specified size.
If size is zero and ptr is not a null pointer, the object pointed to
is freed.
calloc() allocates space for an array of nelem elements of size
elsize. The space is initialized to zeros.
The order and contiguity of storage allocated by successive calls to
calloc() is unspecified. The pointer returned if the allocation
succeeds is suitably aligned so that it may be assigned to a pointer
to any type of object and then used to access such an object or an
array of such objects in the space allocated (until the space is
explicitly freed or reallocated). Each such allocation will yield a
pointer to an object disjoint from any other object. The pointer
returned points to the start (lowest byte address) of the allocated
space. If the space cannot be allocated, a null pointer is returned.
If the size of the space requested is zero, the behavior is
Page 1 Reliant UNIX 5.44 Printed 11/98
malloc(3C) malloc(3C)
implementation-dependent; the value returned will be either a null
pointer or a unique pointer.
memalign() allocates size bytes on a specified alignment boundary, and
returns a pointer to the allocated block. The value of the returned
address is guaranteed to be an even multiple of alignment. Note: the
value of alignment must be a power of two, and must be greater than or
equal to the size of a word.
valloc(size) is equivalent to
memalign(sysconf(SCPAGESIZE),size).
Each of the allocation routines returns a pointer to space suitably
aligned for storage of any type of object.
malloc(), realloc(), calloc(), memalign(), and valloc() will fail if
there is not enough available memory.
RESULT
If there is no available memory, malloc(), realloc(), calloc(),
memalign(), and valloc() return a null pointer. When realloc() returns
NULL, the block pointed to by ptr is left intact. A pointer to the
requested space is returned.
If size is 0 for malloc(), or nelem or elsize is 0 for calloc(), a
pointer with a unique value is returned, and this can be passed to
free().
If size is 0 for realloc(), the space from the address ptr is released
again. The address must be a valid one, as returned by malloc().
NOTES
The algorithm used by malloc(3C) is a "best-fit" algorithm. If you
want to use the "first-fit" algorithm, you should use malloc(3X).
Applications that use the memalign and valloc routines, must not link
in the libmalloc library [see malloc(3X)], because these routines are
based on the malloc(3C) algorithm.
SEE ALSO
mmap(2), malloc(3X).
Page 2 Reliant UNIX 5.44 Printed 11/98