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