MALLOC(S) UNIX System V MALLOC(S)
Name
malloc, free, realloc, calloc - allocates main memory
Syntax
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;
Description
There are two versions of the malloc(S) package. Both
versions are documented in these malloc(S) manual pages; the
description for the other package starts on page 3. This
portion of the manual page documents the standard, default
malloc(S) package. This version of malloc and free provide
a simple general-purpose memory allocation package. malloc
returns a pointer to a block of at least size bytes
beginning on a word boundary.
The argument to free is a pointer to a block previously
allocated by malloc; this space is made available for
further allocation, but its contents are left undisturbed.
Undefined results will occur if space assigned by malloc is
overrun or if some random number is handed to free.
malloc allocates the first contiguous reach of free space
found in a circular search from the last block allocated or
freed, coalescing adjacent free blocks as it searches. It
calls sbrk (see sbrk(S)) to get more memory from the system
when there is no suitable space already 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 will be unchanged up to the lesser of
the new and old sizes. If no free block of size bytes is
available in the storage arena, then realloc will ask malloc
to enlarge the arena by size bytes and will then move the
data to the new space.
realloc also works if ptr points to a block freed since the
last call of malloc, realloc, or calloc; thus sequences of
free, malloc and realloc can exploit the search strategy of
malloc to do storage compaction.
calloc allocates space for an array of nelem elements of
size elsize. The space is initialized to zeros.
Each of the allocation routines returns a pointer to space
suitably aligned (after possible pointer coercion) for
storage of any type of object.
See Also
brkctl(S), malloc(S), sbrk(S)
Diagnostics
malloc, realloc and calloc return a null pointer (0) if
there is no available memory or if the area has been
detectably corrupted by storing outside the bounds of a
block. When realloc returns 0, the block pointed to by ptr
may be destroyed.
Note
As noted, malloc calls sbrk to allocate memory. Since sbrk
takes a signed integer as its argument, malloc will fail if
an attempt is made to allocate more memory than a signed
integer will hold (32K -1).
Search time increases when many objects have been allocated;
that is, if a program allocates but never frees, then each
successive allocation takes longer. For an alternate and
more flexible implementation see the malloc(S) documented
below.
MALLOC(S) UNIX System V MALLOC(S)
Name
malloc, free, realloc, calloc, mallopt, mallinfo - allocates
main memory quickly
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()
Description
There are two versions of the malloc(S) package. This is
the library version which provides a simple general-purpose
memory allocation package, that runs considerably faster
than the other malloc(S) package. Both versions are
documented in these malloc(S) manual pages; the description
of the standard default package starts on page 1.
This malloc(S) package is found in the library ``malloc''
and is loaded when the option -lmalloc is used with cc(CP)
or ld(CP).
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
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.
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 will be unchanged up to the lesser of
the new and old sizes.
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:
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. 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 multiple of grain. 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
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.
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 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.
Here is an example program code segment for the mallinfo
function:
#include <stdio.h>
#include <malloc.h>
main()
{
char *malloc, *cp;
struct mallinfo minfo;
if ((cp = malloc(1024)) == NULL)
{
perror("Malloc");
exit(1);
}
minfo = mallinfo();
printf("%d %d %d0, minfo.arena, minfo.ordblks, minfo.uordblks);
}
Each of the allocation routines returns a pointer to space
suitably aligned (after possible pointer coercion) for
storage of any type of object.
See Also
Programmer's Guide
brkctl(S), malloc(S), sbrk(S)
Diagnostics
malloc, realloc and calloc return a NULL 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 are invalid,
non-zero is returned. Otherwise, it returns zero.
Warnings
This package usually uses more data space than the other
malloc(S).
The code size is also bigger than the other malloc(S).
Note that unlike the other malloc(S), 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 the other malloc(S) have not been
duplicated.
These routines must be linked with the -lmalloc linker
option.
Standards Conformance
calloc, free, malloc and realloc are conformant with:
AT&T SVID Issue 2, Select Code 307-127;
The X/Open Portability Guide II of January 1987;
ANSI X3.159-198X C Language Draft Standard, May 13,
1988;
IEEE POSIX Std 1003.1-1988 with C Standard Language-
Dependent System Support;
and NIST FIPS 151-1.
mallinfo and mallopt are conformant with:
AT&T SVID Issue 2, Select Code 307-127;
and The X/Open Portability Guide II of January 1987.