Bell 32V release
[unix-history] / usr / man / man3 / malloc.3
CommitLineData
f84a56bb
TL
1.TH MALLOC 3
2.SH NAME
3malloc, free, realloc, calloc \- main memory allocator
4.SH SYNOPSIS
5.B char *malloc(size)
6.br
7.B unsigned size;
8.PP
9.B free(ptr)
10.br
11.B char *ptr;
12.PP
13.B char *realloc(ptr, size)
14.br
15.B char *ptr;
16.br
17.B unsigned size;
18.PP
19.B char *calloc(nelem, elsize)
20.br
21.B unsigned nelem, elsize;
22.SH DESCRIPTION
23.I Malloc
24and
25.I free
26provide a simple general-purpose memory allocation package.
27.I Malloc
28returns a pointer to a block of at least
29.I size
30bytes beginning on a word boundary.
31.PP
32The argument to
33.I free
34is a pointer to a block previously allocated by
35.IR malloc ;
36this space is made available for further allocation,
37but its contents are left undisturbed.
38.PP
39Needless to say, grave disorder will result if the space
40assigned by
41.I malloc
42is overrun or if some random number is handed to
43.IR free .
44.PP
45.I Malloc
46allocates the first big enough contiguous reach of
47free space
48found in a circular search from the last
49block allocated or freed,
50coalescing adjacent free blocks as it searches.
51It calls
52.I sbrk
53(see
54.IR break (2))
55to get more memory from the system when there is no
56suitable space already free.
57.PP
58.I Realloc
59changes the size of the block pointed to by
60.I ptr
61to
62.I size
63bytes and returns a pointer to the (possibly moved)
64block.
65The contents will be unchanged up to the
66lesser of the new and old sizes.
67.PP
68.I Realloc
69also works if
70.I ptr
71points to a block freed since the last call of
72.I malloc, realloc
73or
74.IR calloc ;
75thus sequences of
76.I free, malloc
77and
78.I realloc
79can exploit the search strategy of
80.I malloc
81to do storage compaction.
82.PP
83.I Calloc
84allocates space for
85an array of
86.I nelem
87elements of size
88.I elsize.
89The space is initialized to zeros.
90.PP
91Each of the allocation routines returns a pointer
92to space suitably aligned (after possible pointer coercion)
93for storage of any type of object.
94.SH DIAGNOSTICS
95.I Malloc, realloc
96and
97.I calloc
98return a null pointer (0) if there is no available memory
99or if the arena has been detectably corrupted by storing outside the bounds
100of a block.
101.I Malloc
102may be recompiled to check the arena very stringently
103on every transaction;
104see the source code.
105.SH BUGS
106When
107.I realloc
108returns 0,
109the block
110pointed to by
111.I ptr
112may be destroyed.