]>
Commit | Line | Data |
---|---|---|
bf5a7247 | 1 | .\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de) |
fea681da | 2 | .\" |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
4 | .\" Permission is granted to make and distribute verbatim copies of this |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
c13182ef | 12 | .\" |
fea681da MK |
13 | .\" Since the Linux kernel and libraries are constantly changing, this |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
c13182ef | 20 | .\" |
fea681da MK |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
c08df37a | 24 | .\" |
fea681da MK |
25 | .\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu) |
26 | .\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701 | |
27 | .\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de) | |
005d4448 | 28 | .\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap(). |
fea681da | 29 | .\" |
bea08fec MK |
30 | .\" FIXME . Review http://austingroupbugs.net/view.php?id=374 |
31 | .\" to see what changes are required on this page. | |
7c5ca666 | 32 | .\" |
460495ca | 33 | .TH MALLOC 3 2015-08-08 "GNU" "Linux Programmer's Manual" |
fea681da | 34 | .SH NAME |
f68512e9 | 35 | malloc, free, calloc, realloc \- allocate and free dynamic memory |
fea681da MK |
36 | .SH SYNOPSIS |
37 | .nf | |
38 | .B #include <stdlib.h> | |
39 | .sp | |
fea681da | 40 | .BI "void *malloc(size_t " "size" ); |
fea681da | 41 | .BI "void free(void " "*ptr" ); |
12ec54b4 | 42 | .BI "void *calloc(size_t " "nmemb" ", size_t " "size" ); |
d9ec0c6a | 43 | .BI "void *realloc(void " "*ptr" ", size_t " "size" ); |
fea681da MK |
44 | .fi |
45 | .SH DESCRIPTION | |
fea681da | 46 | .PP |
12ec54b4 | 47 | The |
63aa9df0 | 48 | .BR malloc () |
12ec54b4 | 49 | function allocates |
fea681da | 50 | .I size |
c13182ef | 51 | bytes and returns a pointer to the allocated memory. |
655d270c | 52 | .IR "The memory is not initialized" . |
840b4581 MK |
53 | If |
54 | .I size | |
bf1c0ede | 55 | is 0, then |
840b4581 MK |
56 | .BR malloc () |
57 | returns either NULL, | |
58 | .\" glibc does this: | |
59 | or a unique pointer value that can later be successfully passed to | |
60 | .BR free (). | |
fea681da | 61 | .PP |
12ec54b4 | 62 | The |
63aa9df0 | 63 | .BR free () |
12ec54b4 | 64 | function frees the memory space pointed to by |
fea681da MK |
65 | .IR ptr , |
66 | which must have been returned by a previous call to | |
63aa9df0 | 67 | .BR malloc (), |
c8624d68 | 68 | .BR calloc (), |
fea681da | 69 | or |
63aa9df0 | 70 | .BR realloc (). |
fea681da | 71 | Otherwise, or if |
0daa9e92 | 72 | .I free(ptr) |
d9bfdb9c | 73 | has already been called before, undefined behavior occurs. |
fea681da MK |
74 | If |
75 | .I ptr | |
8478ee02 | 76 | is NULL, no operation is performed. |
fea681da | 77 | .PP |
12ec54b4 MK |
78 | The |
79 | .BR calloc () | |
80 | function allocates memory for an array of | |
81 | .I nmemb | |
82 | elements of | |
83 | .I size | |
84 | bytes each and returns a pointer to the allocated memory. | |
85 | The memory is set to zero. | |
86 | If | |
87 | .I nmemb | |
88 | or | |
89 | .I size | |
90 | is 0, then | |
91 | .BR calloc () | |
92 | returns either NULL, | |
93 | .\" glibc does this: | |
94 | or a unique pointer value that can later be successfully passed to | |
95 | .BR free (). | |
96 | .PP | |
97 | The | |
63aa9df0 | 98 | .BR realloc () |
12ec54b4 | 99 | function changes the size of the memory block pointed to by |
fea681da MK |
100 | .I ptr |
101 | to | |
102 | .I size | |
103 | bytes. | |
b4b57a95 | 104 | The contents will be unchanged in the range from the start of the region |
655d270c MK |
105 | up to the minimum of the old and new sizes. |
106 | If the new size is larger than the old size, the added memory will | |
107 | .I not | |
108 | be initialized. | |
fea681da MK |
109 | If |
110 | .I ptr | |
c4acc689 | 111 | is NULL, then the call is equivalent to |
865c9fd8 MK |
112 | .IR malloc(size) , |
113 | for all values of | |
114 | .IR size ; | |
c13182ef MK |
115 | if |
116 | .I size | |
054fccc0 | 117 | is equal to zero, |
c4acc689 MK |
118 | and |
119 | .I ptr | |
120 | is not NULL, then the call is equivalent to | |
840b4581 | 121 | .IR free(ptr) . |
fea681da MK |
122 | Unless |
123 | .I ptr | |
8478ee02 | 124 | is NULL, it must have been returned by an earlier call to |
63aa9df0 MK |
125 | .BR malloc (), |
126 | .BR calloc () | |
fea681da | 127 | or |
63aa9df0 | 128 | .BR realloc (). |
fea681da | 129 | If the area pointed to was moved, a |
0daa9e92 | 130 | .I free(ptr) |
fea681da | 131 | is done. |
47297adb | 132 | .SH RETURN VALUE |
12ec54b4 MK |
133 | The |
134 | .BR malloc () | |
c13182ef | 135 | and |
12ec54b4 | 136 | .BR calloc () |
25630b27 GP |
137 | functions return a pointer to the allocated memory, |
138 | which is suitably aligned for any built-in type. | |
fdc6b831 MK |
139 | On error, these functions return NULL. |
140 | NULL may also be returned by a successful call to | |
141 | .BR malloc () | |
142 | with a | |
143 | .I size | |
144 | of zero, | |
145 | or by a successful call to | |