1MALLOC(3)                  Library Functions Manual                  MALLOC(3)
2
3
4

NAME

6       malloc, free, realloc, calloc, alloca - memory allocator
7

SYNOPSIS

9       char *malloc(size)
10       unsigned size;
11
12       free(ptr)
13       char *ptr;
14
15       char *realloc(ptr, size)
16       char *ptr;
17       unsigned size;
18
19       char *calloc(nelem, elsize)
20       unsigned nelem, elsize;
21
22       char *alloca(size)
23       int size;
24

DESCRIPTION

26       Malloc  and  free  provide a general-purpose memory allocation package.
27       Malloc returns a pointer to a block of at least size bytes beginning on
28       a word boundary.
29
30       The  argument  to  free is a pointer to a block previously allocated by
31       malloc; this space is made available for further  allocation,  but  its
32       contents are left undisturbed.
33
34       Needless  to  say,  grave disorder will result if the space assigned by
35       malloc is overrun or if some random number is handed to free.
36
37       Malloc maintains multiple lists of free blocks according to size, allo‐
38       cating  space from the appropriate list.  It calls sbrk (see brk(2)) to
39       get more memory from the system when there is no suitable space already
40       free.
41
42       Realloc  changes  the size of the block pointed to by ptr to size bytes
43       and returns a pointer to the (possibly moved) block.  The contents will
44       be unchanged up to the lesser of the new and old sizes.
45
46       In  order  to  be compatible with older versions, realloc also works if
47       ptr points to a block freed since the last call of malloc,  realloc  or
48       calloc;  sequences  of free, malloc and realloc were previously used to
49       attempt storage compaction.  This procedure is no longer recommended.
50
51       Calloc allocates space for an array of nelem elements of  size  elsize.
52       The space is initialized to zeros.
53
54       Alloca  allocates size bytes of space in the stack frame of the caller.
55       This temporary space is automatically freed on return.
56
57       Each of the allocation routines returns a  pointer  to  space  suitably
58       aligned  (after  possible  pointer coercion) for storage of any type of
59       object.  If the space is of pagesize or  larger,  the  memory  returned
60       will be page-aligned.
61

SEE ALSO

63       brk(2), pagesize(2)
64

DIAGNOSTICS

66       Malloc,  realloc  and  calloc  return a null pointer (0) if there is no
67       available memory or if the arena has been detectably corrupted by stor‐
68       ing  outside  the bounds of a block.  Malloc may be recompiled to check
69       the arena very stringently on every transaction;  those  sites  with  a
70       source  code  license  may check the source code to see how this can be
71       done.
72

BUGS

74       When realloc returns 0, the block pointed to by ptr may be destroyed.
75
76       The current implementation of malloc does not  always  fail  gracefully
77       when system memory limits are approached.  It may fail to allocate mem‐
78       ory when larger free blocks could be broken  up,  or  when  limits  are
79       exceeded  because  the  size  is rounded up.  It is optimized for sizes
80       that are powers of two.
81
82       Alloca is machine dependent; its use is discouraged.
83
84
85
864th Berkeley Distribution        May 14, 1986                        MALLOC(3)
Impressum