malloc(3P) INTERACTIVE UNIX System (POSIX) malloc(3P)
NAME
malloc, free, realloc, calloc - memory allocator
SYNOPSIS
#include <stdlib.h>
void *malloc(size_t size);
#include <stdlib.h>
void free(void *ptr);
#include <stdlib.h>
void *realloc(void *ptr, size_t size);
#include <stdlib.h>
void *calloc(size_t nmemb, size_t size);
DESCRIPTION
The malloc function allocates space for an object whose size
is specified by size and whose value is indeterminate.
The free function causes the space pointed to by ptr to be
deallocated, that is, made available for further allocation.
If ptr is a null pointer, no action occurs. Otherwise, if
the argument does not match a pointer earlier returned by
the calloc, malloc, or realloc functions, or if the space
has been deallocated by a call to free or realloc, the
behavior is undefined.
The realloc function changes the size of the object pointed
to by ptr to the size specified by size. The contents of
the object shall be unchanged up to the lesser of the new
and old sizes. If the new size is larger, the value of the
newly allocated portion portion of the object is indeter-
minate. If ptr is a null pointer, the realloc function
behaves like the malloc function for the specified size.
Otherwise, if ptr does not match a pointer earlier returned
by the calloc, malloc, or realloc functions, or if the space
has been deallocated by a call to the free or realloc func-
tions, the behavior is undefined. If the space cannot be
allocated, the object pointed to by ptr is unchanged. If
size is zero and ptr is not a null pointer, the object it
points to is freed.
The calloc function allocates space for an array of nmemb
objects, each of whose size is size. The space is initial-
ized to all bits zero. Note that this need not be the same
as the representation of floating-point zero or a null
pointer constant.
The order and contiguity of storage allocated by successive
calls to the calloc, malloc, and realloc functions is
unspecified. The pointer returned if the allocation
succeeds is suitably aligned so that it may be assigned to a
pointer to any type of object and then used to access such
an object or an array of such objects in the space allocated
(until the space is explicitly freed or reallocated). Each
Rev. 1.1 Page 1
malloc(3P) INTERACTIVE UNIX System (POSIX) malloc(3P)
such allocation shall yield a pointer to an object disjoint
from any other object. The pointer returned points to the
start (lowest byte address) of the allocated space. If the
space cannot be allocated, a null pointer is returned. If
the size of the space requested is zero, the value returned
shall be a unique pointer. The value of a pointer that
refers to freed space is indeterminate.
RETURN VALUES
The malloc function returns either a null pointer or a
pointer to the allocated space.
The free function returns no value.
The realloc function returns either a null pointer or a
pointer to the possibly moved allocated space.
The calloc function returns either a null pointer or a
pointer to the allocated space.
NOTES
If an ANSI C compiler is not available, then the type char *
may be used in place of void *.
Rev. 1.1 Page 2