1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
.\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" License.
.\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
.\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
.\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
.\" 2007-09-15 mtk: added notes on malloc()'s use of sbrk() and mmap().
.\"
.\" Japanese Version Copyright (c) 1998 NAKANO Takeo all rights reserved.
.\" Translated 1998-08-12, NAKANO Takeo <nakano@apm.seikei.ac.jp>
.\" Modified 1998-12-18, NAKANO Takeo
.\" Modified 2000-08-23, NAKANO Takeo
.\" Modified 2001-05-19, Kentaro Shirakata <argrath@ub32.org>
.\" Modified 2001-12-13, Kentaro Shirakata <argrath@ub32.org>
.\" Modified 2005-03-15, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Modified 2007-10-12, Akihiro MOTOKI, LDP v2.66
.\"
.\" WORD: OOM killer
.\"
.TH MALLOC 3 2008-02-22 "GNU" "Linux Programmer's Manual"
.SH ̾
calloc, malloc, free, realloc \- ưŪʥγƤȲԤ
.SH
.nf
.B #include <stdlib.h>
.sp
.BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
.br
.BI "void *malloc(size_t " "size" );
.br
.BI "void free(void " "*ptr" );
.br
.BI "void *realloc(void " "*ptr" ", size_t " "size" );
.fi
.SH
.BR calloc ()
.I size
ХȤ
.I nmemb
Ĥʤ˥ơ
Ƥ줿Фݥ֤
ƤϿͥ (ӥåȤΥХ) ˥åȤ롣
.I nmemb
.I size
0 ξ硢
.BR calloc ()
NULL
.\" glibc Ϥ
ޤ
.BR free ()
˸ϤƤεʤդʥݥ֤ͤ
.PP
.BR malloc ()
.I size
ХȤơ
Ƥ줿Фݥ֤
Ƥϥꥢʤ
.I size
0 ξ硢
.BR malloc ()
NULL
.\" glibc Ϥ
ޤ
.BR free ()
˸ϤƤεʤդʥݥ֤ͤ
.PP
.BR free ()
ϥݥ
.I ptr
ؤ֤롣Υݥϡ˸ƤӽФ줿
.BR malloc (),
.BR calloc (),
.BR realloc ()
Τ줫֤ͤǤʤФʤʤ
ʳΥݥꤷꡢǤ
.I free(ptr)
¹ԤƤꤷưƤʤ
.I ptr
NULL ξˤϡʤưԤʤ
.PP
.BR realloc ()
ϡݥ
.I ptr
֥åΥѹ
.I size
ХȤˤ롣
ΥΤۤΥ֥å˴ޤޤƤѹʤ
Ƥ줿ƤϽʤ
.I size
ɤͤǤäƤ⡢
.I ptr
NULL ξˤ
.I malloc(size)
Ǥ롣
.I size
0
.I ptr
NULL Ǥʤˤϡ
.I free(ptr)
Ǥ롣
.I ptr
NULL ʳξ硢
.I ptr
ϰ˸ƤӽФ줿
.BR malloc (),
.BR calloc (),
.BR realloc ()
Τ줫֤ͤǤʤФʤʤ
.I ptr
ؤΰ褬ưƤ
.I free(ptr)
¹Ԥ롣
.SH ֤
.BR calloc ()
.BR malloc ()
֤ͤϡƤ줿ؤΥݥǤ롣
ϤѿбǤ褦˥饤ȤƤ롣
˼Ԥ NULL ֤롣
.PP
.BR free ()
֤ͤʤ
.PP
.BR realloc ()
Ͽ˳Ƥ줿ؤΥݥ֤
ϤѿбǤ褦˥饤ȤƤꡢ
.I ptr
Ȥϰۤʤ뤳Ȥ⤢롣
˼Ԥ NULL ֤롣
.I size
0 ξˤϡNULL ⤷
.BR free ()
ϤȤǤݥ֤롣
.BR realloc ()
ԤˤϡΥ֥åѹʤ
Ĥޤꡢ줿ư줿Ϥʤ
.SH
C89, C99.
.SH
̾
.BR malloc ()
ϡҡפơɬפ˱ƥҡפΥ
.BR sbrk (2)
ȤäĴ᤹롣
.B MMAP_THRESHOLD
ХȤ礭ʥ֥åƤ硢
glibc
.BR malloc ()
.BR mmap (2)
Ȥäƥץ饤١Ȥ̵̾ޥåԥ (anonymous mapping) Ȥ
Ƥ롣
ǥեȤǤ
.B MMAP_THRESHOLD
128 kB
.BR mallopt (3)
ȤäĴǤ롣
.\" FIXME . there is no mallopt(3) man page yet.
.BR mmap (2)
ȤäƹԤ줿Ƥ
.B RLIMIT_DATA
¤αƶʤ
.RB ( getrlimit (2)
)
Unix98 ɸǤϡ
.BR malloc (),
.BR calloc (),
.BR realloc ()
ϼ¹Ԥ˼ԤȤ
.I errno
.B ENOMEM
ꤹ뤳ȤˤʤäƤ롣
Glibc ǤϤ줬Ƥ뤳ȤꤷƤ
(ޤΥ롼 glibc СϤΤȤäƤ)
ĿṲ̄ malloc μȤäƤơ malloc
.I errno
ꤷʤˤϡԤݤ
.I errno
˥顼ͳꤷʤ饤֥롼뤫⤷ʤ
.LP
.BR malloc (),
.BR calloc (),
.BR realloc (),
.BR free ()
ˤΤϡ
ۤȤɤξϥҡפ˲ (corruption) Ǥ롣
㤨СƤ줿ΰСե롢
Ʊݥ free 롢ʤɤˤ롣
.PP
ǶΥС Linux libc (5.4.23 ʹ) glibc (2.x) Ǥϡ
.BR malloc ()
ưĶѿˤäǤ褦ʼƤ롣
.B MALLOC_CHECK_
ꤵƤȡüʼѤ졢
ñʥ顼ˤѤ뤳ȤǤ褦ˤʤ (Ψϰʤ)㤨С
.BR free ()
ƱٸƤӽФƤޤ
1 ХȤԤƤޤ (off-by-one Х) ʤɤ롣
Υ顼ƤɤȤǤ櫓ǤϤʤ
ξˤϥäƤޤ
.B MALLOC_CHECK_
0 ˥åȤƤȡҡפ˲ۤä̵뤹롣
1 ˥åȤƤȡǥåɸ२顼Ϥɽ롣
2 ˥åȤƤȡ
.BR abort (3)
ƤӽФ롣
3 ˥åȤƤȡǥåɸ२顼Ϥɽ졢
ץ϶λ (abort) 롣
.B MALLOC_CHECK_
0 ʳͤåȤΩľȤƤϡ
ºݤΥץΥå夬äȸ˵ꡢ
θõФΤ˺ʾʤɤ
.SH Х
ǥեȤǤϡLinux ϳڴŪάѤƤ롣
Ĥޤꡢ
.BR malloc ()
NULL Ǥʤ֤ͤƤ⡢Υ꤬ºݤѲǽǤ뤳Ȥ
ݾڤʤˤޤХǤ롣
ƥब֤ˤʤäȤ̾⤤ (OOM killer)
ˤäưĤޤʣΥץ롣
ץΤ˾ޤʤǻѤƤơ
⥫ͥΥСʬ˺ǶΤΤǤСΥ
Ʋư (overcommitting behavior) ʲΥޥɤ
̵ˤǤ롣
.in +4n
.sp
.nf
# echo 2 > /proc/sys/vm/overcommit_memory
.fi
.in
ͥ°ʸ
.I vm/overcommit-accounting
.I sysctl/vm.txt
⻲ȤΤȡ
.SH Ϣ
.BR brk (2),
.BR posix_memalign (3)
.\" .BR mallopt (3),
.BR mmap (2),
.BR alloca (3),
|