cc [ flag ... ] file ... -lmalloc [ library ... ]
#include <stdlib.h>
void *malloc(size_t size);
void free(void *ptr);
void *realloc(void *ptr, size_t size);
void *calloc(size_t nelem, size_t elsize);
#include <malloc.h>
int mallopt(int cmd, int value);
struct mallinfo mallinfo(void);
Safe
malloc() 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() is performed this space is made available for further allocation, and its contents have been destroyed (but see mallopt() below for a way to change this behavior). If ptr is a null pointer, no action occurs.
Undefined results 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 contents are unchanged up to the lesser of the new and old sizes. If ptr is a null pointer, realloc() behaves like malloc() for the specified size. If size is zero and ptr is not a null pointer, the object it points to is freed.
calloc() allocates space for an array of nelem elements of size elsize. The space is initialized to zeros.
mallopt() provides for control over the allocation algorithm. The available values for cmd are:
These values are defined in the <malloc.h> header.
mallopt() may be called repeatedly, but may not be called after the first small block is allocated.
mallinfo() provides instrumentation describing space usage. It returns the mallinfo structure with the following members:
."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 */
The mallinfo structure is defined in the <malloc.h> header.
Each of the allocation routines returns a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object.
Note that unlike malloc(3C) , this package does not preserve the contents of a block when it is freed, unless the M_KEEP option of mallopt() is used.
Undocumented features of malloc(3C) have not been duplicated.
Function
prototypes for malloc(), realloc(), calloc(), and free() are also defined
in the <malloc.h> header for compatibility with old applications. New applications
should include <stdlib.h> to access the prototypes for these functions.
Comparative Features of malloc(3X)
, bsdmalloc(3X)
, and malloc(3C)
:
- These malloc(3X) routines are space-efficient, but have slower performance.
- The bsdmalloc(3X) routines afford better performance, but are space-inefficient.
- The standard, fully SCD-compliant malloc(3C) routines are a trade-off between performance and space-efficiency.
free() does not set errno.