1*a948ccc5Swiz.\" $NetBSD: malloc.3,v 1.48 2016/06/01 08:32:05 wiz Exp $ 27d88f2f5Swiz.\" 35f8a6c06Sperry.\" Copyright (c) 1980, 1991, 1993 45f8a6c06Sperry.\" The Regents of the University of California. All rights reserved. 561f28255Scgd.\" 661f28255Scgd.\" This code is derived from software contributed to Berkeley by 761f28255Scgd.\" the American National Standards Committee X3, on Information 861f28255Scgd.\" Processing Systems. 961f28255Scgd.\" 1061f28255Scgd.\" Redistribution and use in source and binary forms, with or without 1161f28255Scgd.\" modification, are permitted provided that the following conditions 1261f28255Scgd.\" are met: 1361f28255Scgd.\" 1. Redistributions of source code must retain the above copyright 1461f28255Scgd.\" notice, this list of conditions and the following disclaimer. 1561f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright 1661f28255Scgd.\" notice, this list of conditions and the following disclaimer in the 1761f28255Scgd.\" documentation and/or other materials provided with the distribution. 18eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors 1961f28255Scgd.\" may be used to endorse or promote products derived from this software 2061f28255Scgd.\" without specific prior written permission. 2161f28255Scgd.\" 2261f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2361f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2461f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2561f28255Scgd.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2661f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2761f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2861f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2961f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3061f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3161f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3261f28255Scgd.\" SUCH DAMAGE. 3361f28255Scgd.\" 34c2601fffSlukem.\" @(#)malloc.3 8.1 (Berkeley) 6/4/93 3581e619b9Sad.\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $ 3661f28255Scgd.\" 3739bee72cSpgoyette.Dd June 1, 2016 3861f28255Scgd.Dt MALLOC 3 3938c96853Sgarbled.Os 4061f28255Scgd.Sh NAME 417347edd3Schristos.Nm malloc , calloc , realloc , free 42c2601fffSlukem.Nd general purpose memory allocation functions 43312aca53Sperry.Sh LIBRARY 44312aca53Sperry.Lb libc 4561f28255Scgd.Sh SYNOPSIS 46472351e1Swiz.In stdlib.h 4761f28255Scgd.Ft void * 4861f28255Scgd.Fn malloc "size_t size" 49c2601fffSlukem.Ft void * 50c2601fffSlukem.Fn calloc "size_t number" "size_t size" 51c2601fffSlukem.Ft void * 52c2601fffSlukem.Fn realloc "void *ptr" "size_t size" 53c2601fffSlukem.Ft void 54c2601fffSlukem.Fn free "void *ptr" 5561f28255Scgd.Sh DESCRIPTION 5661f28255ScgdThe 5761f28255Scgd.Fn malloc 58c2601fffSlukemfunction allocates 59c2601fffSlukem.Fa size 6081e619b9Sadbytes of uninitialized memory. 61c2601fffSlukemThe allocated space is suitably aligned (after possible pointer coercion) 62c2601fffSlukemfor storage of any type of object. 63c2601fffSlukem.Pp 64c2601fffSlukemThe 65c2601fffSlukem.Fn calloc 66c2601fffSlukemfunction allocates space for 67c2601fffSlukem.Fa number 68c2601fffSlukemobjects, 69c2601fffSlukemeach 70c2601fffSlukem.Fa size 71c2601fffSlukembytes in length. 72c2601fffSlukemThe result is identical to calling 73c2601fffSlukem.Fn malloc 74c2601fffSlukemwith an argument of 75c2601fffSlukem.Dq "number * size" , 7681e619b9Sadwith the exception that the allocated memory is explicitly initialized 777347edd3Schristosto zero bytes. 78c2601fffSlukem.Pp 79c2601fffSlukemThe 80c2601fffSlukem.Fn realloc 81c2601fffSlukemfunction changes the size of the previously allocated memory referenced by 82c2601fffSlukem.Fa ptr 83c2601fffSlukemto 84c2601fffSlukem.Fa size 8581e619b9Sadbytes. 86c2601fffSlukemThe contents of the memory are unchanged up to the lesser of the new and 87c2601fffSlukemold sizes. 88c2601fffSlukemIf the new size is larger, 89c2601fffSlukemthe value of the newly allocated portion of the memory is undefined. 9081e619b9SadUpon success, the memory referenced by 91c2601fffSlukem.Fa ptr 9281e619b9Sadis freed and a pointer to the newly allocated memory is returned. 93a582ce59Schristos.Pp 9481e619b9SadNote that 9581e619b9Sad.Fn realloc 9681e619b9Sadmay move the memory allocation, resulting in a different return value than 9781e619b9Sad.Fa ptr . 98c2601fffSlukemIf 99c2601fffSlukem.Fa ptr 10060bfa780Swizis 10160bfa780Swiz.Dv NULL , 10260bfa780Swizthe 103c2601fffSlukem.Fn realloc 104c2601fffSlukemfunction behaves identically to 105c2601fffSlukem.Fn malloc 106c2601fffSlukemfor the specified size. 1078acd3a71Syamt.Pp 108c2601fffSlukemThe 109c2601fffSlukem.Fn free 110c2601fffSlukemfunction causes the allocated memory referenced by 111c2601fffSlukem.Fa ptr 112c2601fffSlukemto be made available for future allocations. 113c2601fffSlukemIf 114c2601fffSlukem.Fa ptr 11560bfa780Swizis 11660bfa780Swiz.Dv NULL , 11760bfa780Swizno action occurs. 11881e619b9Sad.Sh RETURN VALUES 11981e619b9SadThe 12081e619b9Sad.Fn malloc 12181e619b9Sadand 12281e619b9Sad.Fn calloc 12381e619b9Sadfunctions return a pointer to the allocated memory if successful; otherwise 12481e619b9Sada 12581e619b9Sad.Dv NULL 12681e619b9Sadpointer is returned and 12781e619b9Sad.Va errno 12881e619b9Sadis set to 12981e619b9Sad.Er ENOMEM . 13081e619b9Sad.Pp 13181e619b9SadThe 13281e619b9Sad.Fn realloc 1337347edd3Schristosfunction returns a pointer, possibly identical to 13481e619b9Sad.Fa ptr , 13581e619b9Sadto the allocated memory 13681e619b9Sadif successful; otherwise a 13781e619b9Sad.Dv NULL 13881e619b9Sadpointer is returned, and 13981e619b9Sad.Va errno 14081e619b9Sadis set to 14181e619b9Sad.Er ENOMEM 14281e619b9Sadif the error was the result of an allocation failure. 14381e619b9SadThe 14481e619b9Sad.Fn realloc 1457347edd3Schristosfunction always leaves the original buffer intact 14689aa62f9Syamtwhen an error occurs. 147*a948ccc5SwizIf 148*a948ccc5Swiz.Ar size 149*a948ccc5Swizis 0, either 150*a948ccc5Swiz.Dv NULL 151*a948ccc5Swizor a pointer that can be safely passed to 152*a948ccc5Swiz.Xr free 3 153*a948ccc5Swizis returned. 15481e619b9Sad.Pp 15581e619b9SadThe 15681e619b9Sad.Fn free 15781e619b9Sadfunction returns no value. 158ef40c024Sjruoho.Sh EXAMPLES 159ef40c024SjruohoWhen using 160ef40c024Sjruoho.Fn malloc , 161ef40c024Sjruohobe careful to avoid the following idiom: 162ef40c024Sjruoho.Bd -literal -offset indent 163ef40c024Sjruohoif ((p = malloc(number * size)) == NULL) 164ef40c024Sjruoho err(EXIT_FAILURE, "malloc"); 165ef40c024Sjruoho.Ed 166ef40c024Sjruoho.Pp 167ef40c024SjruohoThe multiplication may lead to an integer overflow. 168ef40c024SjruohoTo avoid this, 1697347edd3Schristos.Xr reallocarr 3 170ef40c024Sjruohois recommended. 171ef40c024Sjruoho.Pp 172ef40c024SjruohoIf 173ef40c024Sjruoho.Fn malloc 174ef40c024Sjruohomust be used, be sure to test for overflow: 175ef40c024Sjruoho.Bd -literal -offset indent 176ef40c024Sjruohoif (size && number > SIZE_MAX / size) { 177ef40c024Sjruoho errno = EOVERFLOW; 178ef40c024Sjruoho err(EXIT_FAILURE, "allocation"); 179ef40c024Sjruoho} 180ef40c024Sjruoho.Ed 181ef40c024Sjruoho.Pp 182a582ce59SchristosThe above test is not sufficient in all cases. 183a582ce59SchristosFor example, multiplying ints requires a different set of checks: 184a582ce59Schristos.Bd -literal -offset indent 185a582ce59Schristosint num, size; 186a582ce59Schristos\&.\&.\&. 187a582ce59Schristos 188a582ce59Schristos/* Avoid invalid requests */ 189a582ce59Schristosif (size < 0 || num < 0) 190a582ce59Schristos errc(1, EOVERFLOW, "overflow"); 191a582ce59Schristos 192a582ce59Schristos/* Check for signed int overflow */ 193a582ce59Schristosif (size && num > INT_MAX / size) 194a582ce59Schristos errc(1, EOVERFLOW, "overflow"); 195a582ce59Schristos 196a582ce59Schristosif ((p = malloc(size * num)) == NULL) 197a582ce59Schristos err(1, "malloc"); 198a582ce59Schristos.Ed 199a582ce59Schristos.Pp 200a582ce59SchristosAssuming the implementation checks for integer overflow as 201a582ce59Schristos.Nx 202a582ce59Schristosdoes, it is much easier to use 203a582ce59Schristos.Fn calloc 204a582ce59Schristosor 2057347edd3Schristos.Xr reallocarr 3 . 206a582ce59Schristos.Pp 207a582ce59SchristosThe above examples could be simplified to: 208a582ce59Schristos.Bd -literal -offset indent 2097347edd3Schristosptr = NULL; 2107347edd3Schristosif ((e = reallocarr(&ptr, num, size))) 2117347edd3Schristos errx(1, "reallocarr", strerror(e)); 212a582ce59Schristos.Ed 213a582ce59Schristos.Bd -literal -offset indent 214a582ce59Schristosor at the cost of initialization: 215a582ce59Schristosif ((p = calloc(num, size)) == NULL) 216a582ce59Schristos err(1, "calloc"); 217a582ce59Schristos.Ed 218a582ce59Schristos.Pp 219ef40c024SjruohoWhen using 220ef40c024Sjruoho.Fn realloc , 221ef40c024Sjruohoone must be careful to avoid the following idiom: 222ef40c024Sjruoho.Bd -literal -offset indent 223ef40c024Sjruohonsize += 50; 224ef40c024Sjruoho 225ef40c024Sjruohoif ((p = realloc(p, nsize)) == NULL) 226ef40c024Sjruoho return NULL; 227ef40c024Sjruoho.Ed 228ef40c024Sjruoho.Pp 229ef40c024SjruohoDo not adjust the variable describing how much memory has been allocated 230ef40c024Sjruohountil it is known that the allocation has been successful. 231ef40c024SjruohoThis can cause aberrant program behavior if the incorrect size value is used. 232ef40c024SjruohoIn most cases, the above example will also leak memory. 233ef40c024SjruohoAs stated earlier, a return value of 234ef40c024Sjruoho.Dv NULL 235ef40c024Sjruohoindicates that the old object still remains allocated. 236ef40c024SjruohoBetter code looks like this: 237ef40c024Sjruoho.Bd -literal -offset indent 238ef40c024Sjruohonewsize = size + 50; 239ef40c024Sjruoho 240ef40c024Sjruohoif ((p2 = realloc(p, newsize)) == NULL) { 241ef40c024Sjruoho 242ef40c024Sjruoho if (p != NULL) 243ef40c024Sjruoho free(p); 244ef40c024Sjruoho 245ef40c024Sjruoho p = NULL; 246ef40c024Sjruoho return NULL; 247ef40c024Sjruoho} 248ef40c024Sjruoho 249ef40c024Sjruohop = p2; 250ef40c024Sjruohosize = newsize; 251ef40c024Sjruoho.Ed 25261f28255Scgd.Sh SEE ALSO 2537d88f2f5Swiz.\" .Xr limits 1 , 25481e619b9Sad.Xr madvise 2 , 25581e619b9Sad.Xr mmap 2 , 25681e619b9Sad.Xr sbrk 2 , 257c9e587d5Swiz.Xr aligned_alloc 3 , 25861f28255Scgd.Xr alloca 3 , 25981e619b9Sad.Xr atexit 3 , 26053609ce7Smikel.Xr getpagesize 3 , 26181e619b9Sad.Xr memory 3 , 2627347edd3Schristos.Xr posix_memalign 3 , 263c9e587d5Swiz.Xr reallocarr 3 26426e9ae25Sjruoho.Pp 26526e9ae25SjruohoFor the implementation details, see 26626e9ae25Sjruoho.Xr jemalloc 3 . 26761f28255Scgd.Sh STANDARDS 26861f28255ScgdThe 269c2601fffSlukem.Fn malloc , 270c2601fffSlukem.Fn calloc , 271c2601fffSlukem.Fn realloc 272c2601fffSlukemand 273c2601fffSlukem.Fn free 274c2601fffSlukemfunctions conform to 27581e619b9Sad.St -isoC . 27613f37060Skamil.Sh HISTORY 27713f37060SkamilA 27813f37060Skamil.Fn free 27913f37060Skamilinternal kernel function and a predecessor to 28013f37060Skamil.Fn malloc , 28113f37060Skamil.Fn alloc , 28213f37060Skamilfirst appeared in 28313f37060Skamil.At v1 . 28413f37060SkamilThe C Library functions 28513f37060Skamil.Fn alloc 28613f37060Skamiland 28713f37060Skamil.Fn free 28813f37060Skamilappeared in 28913f37060Skamil.At v6 . 29013f37060SkamilThe functions 29113f37060Skamil.Fn malloc , 29213f37060Skamil.Fn calloc , 29313f37060Skamiland 29413f37060Skamil.Fn realloc 29513f37060Skamilfirst appeared in 29613f37060Skamil.At v7 . 29713f37060Skamil.Pp 29813f37060SkamilA new implementation by Chris Kingsley was introduced in 29913f37060Skamil.Bx 4.2 , 300d54956feSwizfollowed by a complete rewrite by Poul-Henning Kamp 301d54956feSwiz.Dq ( phk's malloc 30213f37060Skamilor 30313f37060Skamil.Dq new malloc ) 30413f37060Skamilwhich appeared in 30513f37060Skamil.Fx 2.2 30613f37060Skamiland was included in 30713f37060Skamil.Nx 1.5 30813f37060Skamiland 30913f37060Skamil.Ox 2.0 . 31013f37060SkamilThese implementations were all 31113f37060Skamil.Xr sbrk 2 31213f37060Skamilbased. 31313f37060Skamil.Pp 31413f37060SkamilThe 315d54956feSwiz.Xr jemalloc 3 31613f37060Skamilallocator became the default system allocator first in 31713f37060Skamil.Fx 7.0 31813f37060Skamiland then in 31913f37060Skamil.Nx 5.0 . 320