pluralize it...
[unix-history] / usr / src / lib / libc / stdlib / malloc.3
CommitLineData
679a4429
KM
1.\" Copyright (c) 1980 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
4.\"
71df903f 5.\" @(#)malloc.3 6.3 (Berkeley) %G%
679a4429 6.\"
2f566ccb 7.TH MALLOC 3 ""
679a4429
KM
8.UC 4
9.SH NAME
a435d0f0 10malloc, free, realloc, calloc, alloca \- memory allocator
679a4429
KM
11.SH SYNOPSIS
12.nf
13.B char *malloc(size)
14.B unsigned size;
15.PP
16.B free(ptr)
17.B char *ptr;
18.PP
19.B char *realloc(ptr, size)
20.B char *ptr;
21.B unsigned size;
22.PP
23.B char *calloc(nelem, elsize)
24.B unsigned nelem, elsize;
a435d0f0
KM
25.PP
26.B char *alloca(size)
27.B int size;
679a4429
KM
28.fi
29.SH DESCRIPTION
30.I Malloc
31and
32.I free
71df903f 33provide a general-purpose memory allocation package.
679a4429
KM
34.I Malloc
35returns a pointer to a block of at least
36.I size
37bytes beginning on a word boundary.
38.PP
39The argument to
40.I free
41is a pointer to a block previously allocated by
42.IR malloc ;
43this space is made available for further allocation,
44but its contents are left undisturbed.
45.PP
a435d0f0 46Needless to say, grave disorder will result if the space assigned by
679a4429
KM
47.I malloc
48is overrun or if some random number is handed to
49.IR free .
50.PP
51.I Malloc
a435d0f0
KM
52maintains multiple lists of free blocks according to size,
53allocating space from the appropriate list.
679a4429
KM
54It calls
55.I sbrk
56(see
a435d0f0 57.IR brk (2))
679a4429
KM
58to get more memory from the system when there is no
59suitable space already free.
60.PP
61.I Realloc
62changes the size of the block pointed to by
63.I ptr
64to
65.I size
a435d0f0
KM
66bytes and returns a pointer to the (possibly moved) block.
67The contents will be unchanged up to the lesser of the new and old sizes.
679a4429 68.PP
a435d0f0
KM
69In order to be compatible with older versions,
70.I realloc
679a4429
KM
71also works if
72.I ptr
73points to a block freed since the last call of
74.I malloc, realloc
75or
76.IR calloc ;
a435d0f0 77sequences of
679a4429
KM
78.I free, malloc
79and
80.I realloc
a435d0f0
KM
81were previously used to attempt storage compaction.
82This procedure is no longer recommended.
679a4429
KM
83.PP
84.I Calloc
a435d0f0 85allocates space for an array of
679a4429
KM
86.I nelem
87elements of size
88.I elsize.
89The space is initialized to zeros.
90.PP
a435d0f0
KM
91.I Alloca
92allocates
93.I size
94bytes of space in the stack frame of the caller.
95This temporary space is automatically freed on
96return.
97.PP
679a4429
KM
98Each of the allocation routines returns a pointer
99to space suitably aligned (after possible pointer coercion)
100for storage of any type of object.
71df903f
MK
101If the space is of
102.I pagesize
103or larger, the memory returned will be page-aligned.
104.SH SEE ALSO
105brk(2),
106pagesize(2)
679a4429
KM
107.SH DIAGNOSTICS
108.I Malloc, realloc
109and
110.I calloc
a435d0f0
KM
111return a null pointer (0) if there is no available memory or if the arena
112has been detectably corrupted by storing outside the bounds of a block.
679a4429 113.I Malloc
a435d0f0
KM
114may be recompiled to check the arena very stringently on every transaction;
115those sites with a source code license may check the source code to see
116how this can be done.
679a4429
KM
117.SH BUGS
118When
119.I realloc
a435d0f0 120returns 0, the block pointed to by
679a4429
KM
121.I ptr
122may be destroyed.
123.PP
71df903f
MK
124The current implementation of
125.I malloc
126does not always fail gracefully when system
127memory limits are approached.
128It may fail to allocate memory when larger free blocks could be broken
129up, or when limits are exceeded because the size is rounded up.
130It is optimized for sizes that are powers of two.
131.PP
a435d0f0 132.I Alloca
77bc57f4 133is machine dependent; its use is discouraged.