malloc(3) CLIX malloc(3)
NAME
malloc, free, realloc, calloc, mallopt, mallinfo - Allocates main memory
using fast main memory allocator
LIBRARY
The Malloc Library (libmalloc.a)
SYNOPSIS
#include <malloc.h>
char *malloc(
unsigned size );
void free(
char *ptr );
char *realloc(
char *ptr ,
unsigned size );
char *calloc(
unsigned nelem ,
unsigned elsize );
int mallopt(
int cmd ,
int value );
struct mallinfo mallinfo(
void );
DESCRIPTION
The functions described in this manual page are based in the Malloc
Library (libmalloc.a). For information about the malloc() function and
related functions based in the Standard C Library (libc.a), see the other
malloc(3) reference manual entry.
The functions malloc() and free() provide a simple general-purpose memory
allocation package, which runs considerably faster than the malloc()
package. It is found in the library malloc, and is loaded if the flag -
lmalloc is used with cc or ld.
The malloc() function returns a pointer to a block of at least size bytes
suitably aligned for any use.
The argument to free() is a pointer to a block previously allocated by
malloc(). After free() has performed, this space is available for further
2/94 - Intergraph Corporation 1
malloc(3) CLIX malloc(3)
allocation, and its contents are destroyed (see mallopt() below for a way
to change this behavior).
Undefined results occur if the space assigned by malloc() is overrun or if
some random number is handed to free().
The realloc() function changes the size of the block pointed to by ptr to
size bytes and returns a pointer to the (possibly moved) block. The
contents are unchanged up to the lesser of the new and old sizes.
The calloc() function allocates space for an array of nelem elements of
size elsize. The space is initialized to zeros.
The mallopt() function provides control over the allocation algorithm.
The available values for cmd are:
[M_MXFAST]
Set maxfast to value. The algorithm allocates all blocks below the
size of maxfast in large groups and then doles them out very
quickly. The default value for maxfast is 48.
[M_NLBLKS]
Set numlblks to value. The above mentioned large groups each
contain numlblks blocks. The numlblks must be greater than 0. The
default value for numlblks is 100.
[M_GRAIN]
Set grain to value. All block sizes smaller than maxfast are
considered to be rounded up to the nearest multiple of grain. The
grain must be greater than 0. The default value of grain is the
smallest number of bytes which allow alignment of any data type.
The value will be rounded up to a multiple of the default when
grain is set.
[M_KEEP]
Preserve data in a freed block until the next malloc(), realloc(),
or calloc(). This option is provided only for compatibility with
the old version of malloc() and is not recommended.
These values are defined in the <malloc.h> header file.
The mallopt() function may be called repeatedly, but may not be called
after the first small block is allocated.
The mallinfo function provides instrumentation describing space usage. It
returns the structure:
struct mallinfo {
int arena; /* total space in arena */
int ordblks; /* number of ordinary blocks */
int smblks; /* number of small blocks */
2 Intergraph Corporation - 2/94
malloc(3) CLIX malloc(3)
int hblkhd; /* space in holding block headers */
int hblks; /* number of holding blocks */
int usmblks; /* space in small blocks in use */
int fsmblks; /* space in free small blocks */
int uordblks; /* space in ordinary blocks in use */
int fordblks; /* space in free ordinary blocks */
int keepcost; /* space penalty if keep option is used */
}
This structure is defined in the <malloc.h> header file. Each of the
allocation functions returns a pointer to space suitably aligned (after
possible pointer coercion) for storage of any type of object.
CAUTIONS
This package usually uses more data space than malloc().
The code size is also bigger than malloc().
Note that unlike malloc(), this package does not preserve the contents of
a block when it is freed, unless the option of mallopt() is used.
Undocumented features of malloc() have not been duplicated.
RETURN VALUES
The functions malloc(), realloc(), and calloc() return a pointer if there
is not enough available memory. When realloc() returns NULL, the block
pointed to by ptr is left intact.
If mallopt() is called after any allocation, or if cmd or value is
invalid, a nonzero returns. Otherwise, a zero returns.
RELATED INFORMATION
Functions: brk(2), malloc(3)
2/94 - Intergraph Corporation 3