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 | .\"
|
---|
5 | .\" @(#)malloc.3 6.3 (Berkeley) 5/14/86
|
---|
6 | .\"
|
---|
7 | .TH MALLOC 3 "May 14, 1986"
|
---|
8 | .UC 4
|
---|
9 | .SH NAME
|
---|
10 | malloc, free, realloc, calloc, alloca \- memory allocator
|
---|
11 | .SH SYNOPSIS
|
---|
12 | .nf
|
---|
13 | .ft B
|
---|
14 | #include <sys/types.h>
|
---|
15 | #include <stdlib.h>
|
---|
16 | #include <alloca.h>
|
---|
17 |
|
---|
18 | void *malloc(size_t \fIsize\fP)
|
---|
19 | void free(void *\fIptr\fP)
|
---|
20 | void *realloc(void *\fIptr\fP, size_t \fIsize\fP)
|
---|
21 | void *calloc(size_t \fInelem\fP, size_t \fIelsize\fP)
|
---|
22 | void *alloca(size_t \fIsize\fP)
|
---|
23 | .ft R
|
---|
24 | .fi
|
---|
25 | .SH DESCRIPTION
|
---|
26 | .B Malloc
|
---|
27 | and
|
---|
28 | .B free
|
---|
29 | provide a general-purpose memory allocation package.
|
---|
30 | .B Malloc
|
---|
31 | returns a pointer to a block of at least
|
---|
32 | .I size
|
---|
33 | bytes beginning on a word boundary.
|
---|
34 | .PP
|
---|
35 | The argument to
|
---|
36 | .B free
|
---|
37 | is a pointer to a block previously allocated by
|
---|
38 | .BR malloc ;
|
---|
39 | this space is made available for further allocation,
|
---|
40 | but its contents are left undisturbed.
|
---|
41 | A call with a null
|
---|
42 | .I ptr
|
---|
43 | is legal and does nothing.
|
---|
44 | .PP
|
---|
45 | Needless to say, grave disorder will result if the space assigned by
|
---|
46 | .B malloc
|
---|
47 | is overrun or if some random number is handed to
|
---|
48 | .BR free .
|
---|
49 | .PP
|
---|
50 | .B Malloc
|
---|
51 | maintains multiple lists of free blocks according to size,
|
---|
52 | allocating space from the appropriate list.
|
---|
53 | It calls
|
---|
54 | .B sbrk
|
---|
55 | (see
|
---|
56 | .BR brk (2))
|
---|
57 | to get more memory from the system when there is no
|
---|
58 | suitable space already free.
|
---|
59 | .PP
|
---|
60 | .B Realloc
|
---|
61 | changes the size of the block pointed to by
|
---|
62 | .I ptr
|
---|
63 | to
|
---|
64 | .I size
|
---|
65 | bytes and returns a pointer to the (possibly moved) block.
|
---|
66 | The contents will be unchanged up to the lesser of the new and old sizes.
|
---|
67 | A call with a null
|
---|
68 | .I ptr
|
---|
69 | is legal and has the same result as
|
---|
70 | .BI malloc( size )\fR.
|
---|
71 | .PP
|
---|
72 | .B Calloc
|
---|
73 | allocates space for an array of
|
---|
74 | .I nelem
|
---|
75 | elements of size
|
---|
76 | .I elsize.
|
---|
77 | The space is initialized to zeros.
|
---|
78 | .PP
|
---|
79 | .B Alloca
|
---|
80 | allocates
|
---|
81 | .I size
|
---|
82 | bytes of space in the stack frame of the caller.
|
---|
83 | This temporary space is automatically freed on
|
---|
84 | return.
|
---|
85 | .PP
|
---|
86 | Each of the allocation routines returns a pointer
|
---|
87 | to space suitably aligned (after possible pointer coercion)
|
---|
88 | for storage of any type of object.
|
---|
89 | .SH SEE ALSO
|
---|
90 | .BR brk (2).
|
---|
91 | .SH DIAGNOSTICS
|
---|
92 | .BR Malloc ,
|
---|
93 | .BR realloc
|
---|
94 | and
|
---|
95 | .B calloc
|
---|
96 | return a null pointer if there is no available memory or if the arena
|
---|
97 | has been detectably corrupted by storing outside the bounds of a block.
|
---|
98 | .SH NOTES
|
---|
99 | Other implementations of
|
---|
100 | .BR malloc ,
|
---|
101 | .BR realloc
|
---|
102 | or
|
---|
103 | .BR calloc
|
---|
104 | may return a null pointer if the size of the requested block is zero. This
|
---|
105 | implementation will always return a zero length block at a unique address,
|
---|
106 | but you should keep in mind that a null return is possible if the program
|
---|
107 | is run to another system and that this should not be mistakenly seen as
|
---|
108 | an error.
|
---|
109 | .SH BUGS
|
---|
110 | When
|
---|
111 | .B realloc
|
---|
112 | returns a null pointer, the block pointed to by
|
---|
113 | .I ptr
|
---|
114 | may be destroyed.
|
---|
115 | .PP
|
---|
116 | .B Alloca
|
---|
117 | is machine dependent; its use is discouraged.
|
---|