malloc(3x)malloc(3x)Name
malloc, free, realloc, calloc, mallopt, mallinfo - fast main memory
allocator
Syntax
#include <malloc.h>
char ∗malloc (size)
unsigned size;
void free (ptr)
char ∗ptr;
char ∗realloc (ptr, size)
char ∗ptr;
unsigned size;
char ∗calloc (nelem, elsize)
unsigned nelem, elsize;
int mallopt (cmd, value)
int cmd, value;
struct mallinfo mallinfo (max)
int max;
Description
The and subroutines provide a simple general-purpose memory allocation
package, which runs considerably faster than the package. It is found
in the library and is loaded if the option is used with or
The subroutine returns a pointer to a block of at least size bytes
suitably aligned for any use.
The argument to is a pointer to a block previously allocated by After
is performed, this space is made available for further allocation, and
its contents have been destroyed. See below for a way to change this
behavior.
Undefined results will occur if the space assigned by is overrun or if
some random number is handed to
The subroutine 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.
The subroutine allocates space for an array of nelem elements of size
elsize. The space is initialized to zeros.
The subroutine provides for 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 0.
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 . The sizes of all blocks smaller than
maxfast are considered to be rounded up to the nearest multi‐
ple of grain . The grain must be greater than 0. The default
value of grain is the smallest number of bytes which will
allow alignment of any data type. 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 or This option
is provided only for compatibility with the old version of and
is not recommended.
These values are defined in the malloc.h header file.
The subroutine may be called repeatedly, but may not be called after
the first small block is allocated.
The subroutine provides information describing space usage. It returns
the following structure:
struct mallinfo {
int arena; /* total space in arena */
int ordblks; /* number of ordinary blocks */
int smblks; /* number of small blocks */
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 routines returns a pointer to space suitably
aligned (after possible pointer coercion) for storage of any type of
object.
Restrictions
This package usually uses more data space than
The code size is also bigger than
Note that unlike this package does not preserve the contents of a block
when it is freed, unless the M_KEEP option of is used.
Undocumented features of have not been duplicated.
Return Values
The and subroutines return a NULL pointer if there is not enough avail‐
able memory. When returns NULL, the block pointed to by ptr is left
intact. If is called after any allocation or if cmd or value are
invalid, nonzero is returned. Otherwise, it returns zero.
See Alsobrk(2), malloc(3)malloc(3x)