malloc(3) CLIX malloc(3)
NAME
malloc, free, realloc, calloc - Allocates main memory
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
char *malloc(
unsigned size );
void free(
char *ptr );
char *realloc(
char *ptr ,
unsigned size );
char *calloc(
unsigned nelem ,
unsigned elsize );
PARAMETERS
elsize Specifies the size of an array element.
nelem Specifies the number of elements in an array.
ptr Points to the allocated block of memory.
size Specifies the number of bytes of memory allocated.
DESCRIPTION
The functions described in this manual page are based in the Standard C
Library (libc.a). For information about the malloc() function and related
functions based in the Malloc Library (libmalloc.a), see the other
malloc(3) reference manual entry.
The malloc() and free() functions provide a simple general-purpose memory
allocation package. 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() is performed, this space is made available for
further allocation, but its contents are left undisturbed.
Undefined results occur if the space assigned by malloc() is overrun or if
a random number is handed to free().
2/94 - Intergraph Corporation 1
malloc(3) CLIX malloc(3)
The malloc() function allocates the first big-enough contiguous reach of
free space (found in a circular search from the last block allocated or
freed) and combines adjacent free blocks as it searches. It calls sbrk()
(see brk()) for more system memory when suitable free space is lacking.
The realloc() function changes the block size 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 no free block
of size bytes exits in the storage arena, realloc() asks malloc() to
enlarge the arena by size bytes and then moves the data to the new space.
The realloc() function 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.
The calloc() function allocates space for an array of nelem elements of
size elsize. The space is initialized to zeros.
Each of the allocation functions returns a pointer to a space suitably
aligned (after possible pointer coercion) for storage of any type of
object.
EXAMPLES
To allot 20 memory bytes pointed to by p:
#include <stdlib.h>
main()
{
char *p;
if ((p = malloc(20)) == NULL) {
printf("allocation error - aborting\n");
exit(0);
}
}
FILES
stdlib.h
NOTES
Search time increases as more objects are allotted; that is, if a program
allots but never frees, successive allocations take longer. For a more
flexible implementation see malloc().
RETURN VALUES
2 Intergraph Corporation - 2/94
malloc(3) CLIX malloc(3)
The malloc(), realloc(), and calloc() functions return a pointer to the
first byte of allotted memory. If insufficient memory exits for the
request or if the area is detectably corrupted by storing outside the
block bounds, a NULL pointer returns.
RELATED INFORMATION
Functions: brk(2), malloc(3)
2/94 - Intergraph Corporation 3