xref: /minix3/lib/libc/stdlib/malloc.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\" $NetBSD: malloc.3,v 1.44 2015/07/26 22:32:03 wiz Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1980, 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by
72fe8fb19SBen Gras.\" the American National Standards Committee X3, on Information
82fe8fb19SBen Gras.\" Processing Systems.
92fe8fb19SBen Gras.\"
102fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
122fe8fb19SBen Gras.\" are met:
132fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras.\"    without specific prior written permission.
212fe8fb19SBen Gras.\"
222fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras.\" SUCH DAMAGE.
332fe8fb19SBen Gras.\"
342fe8fb19SBen Gras.\"     @(#)malloc.3	8.1 (Berkeley) 6/4/93
352fe8fb19SBen Gras.\" $FreeBSD: src/lib/libc/stdlib/malloc.3,v 1.73 2007/06/15 22:32:33 jasone Exp $
362fe8fb19SBen Gras.\"
37*0a6a1f1dSLionel Sambuc.Dd July 26, 2015
382fe8fb19SBen Gras.Dt MALLOC 3
392fe8fb19SBen Gras.Os
402fe8fb19SBen Gras.Sh NAME
412fe8fb19SBen Gras.Nm malloc , calloc , realloc , free
422fe8fb19SBen Gras.Nd general purpose memory allocation functions
432fe8fb19SBen Gras.Sh LIBRARY
442fe8fb19SBen Gras.Lb libc
452fe8fb19SBen Gras.Sh SYNOPSIS
462fe8fb19SBen Gras.In stdlib.h
472fe8fb19SBen Gras.Ft void *
482fe8fb19SBen Gras.Fn malloc "size_t size"
492fe8fb19SBen Gras.Ft void *
502fe8fb19SBen Gras.Fn calloc "size_t number" "size_t size"
512fe8fb19SBen Gras.Ft void *
522fe8fb19SBen Gras.Fn realloc "void *ptr" "size_t size"
532fe8fb19SBen Gras.Ft void
542fe8fb19SBen Gras.Fn free "void *ptr"
552fe8fb19SBen Gras.Sh DESCRIPTION
562fe8fb19SBen GrasThe
572fe8fb19SBen Gras.Fn malloc
582fe8fb19SBen Grasfunction allocates
592fe8fb19SBen Gras.Fa size
602fe8fb19SBen Grasbytes of uninitialized memory.
612fe8fb19SBen GrasThe allocated space is suitably aligned (after possible pointer coercion)
622fe8fb19SBen Grasfor storage of any type of object.
632fe8fb19SBen Gras.Pp
642fe8fb19SBen GrasThe
652fe8fb19SBen Gras.Fn calloc
662fe8fb19SBen Grasfunction allocates space for
672fe8fb19SBen Gras.Fa number
682fe8fb19SBen Grasobjects,
692fe8fb19SBen Graseach
702fe8fb19SBen Gras.Fa size
712fe8fb19SBen Grasbytes in length.
722fe8fb19SBen GrasThe result is identical to calling
732fe8fb19SBen Gras.Fn malloc
742fe8fb19SBen Graswith an argument of
752fe8fb19SBen Gras.Dq "number * size" ,
762fe8fb19SBen Graswith the exception that the allocated memory is explicitly initialized
772fe8fb19SBen Grasto zero bytes.
782fe8fb19SBen Gras.Pp
792fe8fb19SBen GrasThe
802fe8fb19SBen Gras.Fn realloc
812fe8fb19SBen Grasfunction changes the size of the previously allocated memory referenced by
822fe8fb19SBen Gras.Fa ptr
832fe8fb19SBen Grasto
842fe8fb19SBen Gras.Fa size
852fe8fb19SBen Grasbytes.
862fe8fb19SBen GrasThe contents of the memory are unchanged up to the lesser of the new and
872fe8fb19SBen Grasold sizes.
882fe8fb19SBen GrasIf the new size is larger,
892fe8fb19SBen Grasthe value of the newly allocated portion of the memory is undefined.
902fe8fb19SBen GrasUpon success, the memory referenced by
912fe8fb19SBen Gras.Fa ptr
922fe8fb19SBen Grasis freed and a pointer to the newly allocated memory is returned.
93*0a6a1f1dSLionel Sambuc.Pp
942fe8fb19SBen GrasNote that
952fe8fb19SBen Gras.Fn realloc
962fe8fb19SBen Grasmay move the memory allocation, resulting in a different return value than
972fe8fb19SBen Gras.Fa ptr .
982fe8fb19SBen GrasIf
992fe8fb19SBen Gras.Fa ptr
1002fe8fb19SBen Grasis
1012fe8fb19SBen Gras.Dv NULL ,
1022fe8fb19SBen Grasthe
1032fe8fb19SBen Gras.Fn realloc
1042fe8fb19SBen Grasfunction behaves identically to
1052fe8fb19SBen Gras.Fn malloc
1062fe8fb19SBen Grasfor the specified size.
1072fe8fb19SBen Gras.Pp
1082fe8fb19SBen GrasThe
1092fe8fb19SBen Gras.Fn free
1102fe8fb19SBen Grasfunction causes the allocated memory referenced by
1112fe8fb19SBen Gras.Fa ptr
1122fe8fb19SBen Grasto be made available for future allocations.
1132fe8fb19SBen GrasIf
1142fe8fb19SBen Gras.Fa ptr
1152fe8fb19SBen Grasis
1162fe8fb19SBen Gras.Dv NULL ,
1172fe8fb19SBen Grasno action occurs.
1182fe8fb19SBen Gras.Sh RETURN VALUES
1192fe8fb19SBen GrasThe
1202fe8fb19SBen Gras.Fn malloc
1212fe8fb19SBen Grasand
1222fe8fb19SBen Gras.Fn calloc
1232fe8fb19SBen Grasfunctions return a pointer to the allocated memory if successful; otherwise
1242fe8fb19SBen Grasa
1252fe8fb19SBen Gras.Dv NULL
1262fe8fb19SBen Graspointer is returned and
1272fe8fb19SBen Gras.Va errno
1282fe8fb19SBen Grasis set to
1292fe8fb19SBen Gras.Er ENOMEM .
1302fe8fb19SBen Gras.Pp
1312fe8fb19SBen GrasThe
1322fe8fb19SBen Gras.Fn realloc
1332fe8fb19SBen Grasfunction returns a pointer, possibly identical to
1342fe8fb19SBen Gras.Fa ptr ,
1352fe8fb19SBen Grasto the allocated memory
1362fe8fb19SBen Grasif successful; otherwise a
1372fe8fb19SBen Gras.Dv NULL
1382fe8fb19SBen Graspointer is returned, and
1392fe8fb19SBen Gras.Va errno
1402fe8fb19SBen Grasis set to
1412fe8fb19SBen Gras.Er ENOMEM
1422fe8fb19SBen Grasif the error was the result of an allocation failure.
1432fe8fb19SBen GrasThe
1442fe8fb19SBen Gras.Fn realloc
1452fe8fb19SBen Grasfunction always leaves the original buffer intact
1462fe8fb19SBen Graswhen an error occurs.
1472fe8fb19SBen Gras.Pp
1482fe8fb19SBen GrasThe
1492fe8fb19SBen Gras.Fn free
1502fe8fb19SBen Grasfunction returns no value.
1512fe8fb19SBen Gras.Sh EXAMPLES
1522fe8fb19SBen GrasWhen using
1532fe8fb19SBen Gras.Fn malloc ,
1542fe8fb19SBen Grasbe careful to avoid the following idiom:
1552fe8fb19SBen Gras.Bd -literal -offset indent
1562fe8fb19SBen Grasif ((p = malloc(number * size)) == NULL)
1572fe8fb19SBen Gras	err(EXIT_FAILURE, "malloc");
1582fe8fb19SBen Gras.Ed
1592fe8fb19SBen Gras.Pp
1602fe8fb19SBen GrasThe multiplication may lead to an integer overflow.
1612fe8fb19SBen GrasTo avoid this,
162*0a6a1f1dSLionel Sambuc.Xr reallocarr 3
1632fe8fb19SBen Grasis recommended.
1642fe8fb19SBen Gras.Pp
1652fe8fb19SBen GrasIf
1662fe8fb19SBen Gras.Fn malloc
1672fe8fb19SBen Grasmust be used, be sure to test for overflow:
1682fe8fb19SBen Gras.Bd -literal -offset indent
1692fe8fb19SBen Grasif (size && number > SIZE_MAX / size) {
1702fe8fb19SBen Gras	errno = EOVERFLOW;
1712fe8fb19SBen Gras	err(EXIT_FAILURE, "allocation");
1722fe8fb19SBen Gras}
1732fe8fb19SBen Gras.Ed
1742fe8fb19SBen Gras.Pp
175*0a6a1f1dSLionel SambucThe above test is not sufficient in all cases.
176*0a6a1f1dSLionel SambucFor example, multiplying ints requires a different set of checks:
177*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent
178*0a6a1f1dSLionel Sambucint num, size;
179*0a6a1f1dSLionel Sambuc\&.\&.\&.
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc/* Avoid invalid requests */
182*0a6a1f1dSLionel Sambucif (size < 0 || num < 0)
183*0a6a1f1dSLionel Sambuc	errc(1, EOVERFLOW, "overflow");
184*0a6a1f1dSLionel Sambuc
185*0a6a1f1dSLionel Sambuc/* Check for signed int overflow */
186*0a6a1f1dSLionel Sambucif (size && num > INT_MAX / size)
187*0a6a1f1dSLionel Sambuc	errc(1, EOVERFLOW, "overflow");
188*0a6a1f1dSLionel Sambuc
189*0a6a1f1dSLionel Sambucif ((p = malloc(size * num)) == NULL)
190*0a6a1f1dSLionel Sambuc	err(1, "malloc");
191*0a6a1f1dSLionel Sambuc.Ed
192*0a6a1f1dSLionel Sambuc.Pp
193*0a6a1f1dSLionel SambucAssuming the implementation checks for integer overflow as
194*0a6a1f1dSLionel Sambuc.Nx
195*0a6a1f1dSLionel Sambucdoes, it is much easier to use
196*0a6a1f1dSLionel Sambuc.Fn calloc
197*0a6a1f1dSLionel Sambucor
198*0a6a1f1dSLionel Sambuc.Xr reallocarr 3 .
199*0a6a1f1dSLionel Sambuc.Pp
200*0a6a1f1dSLionel SambucThe above examples could be simplified to:
201*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent
202*0a6a1f1dSLionel Sambucptr = NULL;
203*0a6a1f1dSLionel Sambucif ((e = reallocarr(&ptr, num, size)))
204*0a6a1f1dSLionel Sambuc	errx(1, "reallocarr", strerror(e));
205*0a6a1f1dSLionel Sambuc.Ed
206*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent
207*0a6a1f1dSLionel Sambucor at the cost of initialization:
208*0a6a1f1dSLionel Sambucif ((p = calloc(num, size)) == NULL)
209*0a6a1f1dSLionel Sambuc	err(1, "calloc");
210*0a6a1f1dSLionel Sambuc.Ed
211*0a6a1f1dSLionel Sambuc.Pp
2122fe8fb19SBen GrasWhen using
2132fe8fb19SBen Gras.Fn realloc ,
2142fe8fb19SBen Grasone must be careful to avoid the following idiom:
2152fe8fb19SBen Gras.Bd -literal -offset indent
2162fe8fb19SBen Grasnsize += 50;
2172fe8fb19SBen Gras
2182fe8fb19SBen Grasif ((p = realloc(p, nsize)) == NULL)
2192fe8fb19SBen Gras	return NULL;
2202fe8fb19SBen Gras.Ed
2212fe8fb19SBen Gras.Pp
2222fe8fb19SBen GrasDo not adjust the variable describing how much memory has been allocated
2232fe8fb19SBen Grasuntil it is known that the allocation has been successful.
2242fe8fb19SBen GrasThis can cause aberrant program behavior if the incorrect size value is used.
2252fe8fb19SBen GrasIn most cases, the above example will also leak memory.
2262fe8fb19SBen GrasAs stated earlier, a return value of
2272fe8fb19SBen Gras.Dv NULL
2282fe8fb19SBen Grasindicates that the old object still remains allocated.
2292fe8fb19SBen GrasBetter code looks like this:
2302fe8fb19SBen Gras.Bd -literal -offset indent
2312fe8fb19SBen Grasnewsize = size + 50;
2322fe8fb19SBen Gras
2332fe8fb19SBen Grasif ((p2 = realloc(p, newsize)) == NULL) {
2342fe8fb19SBen Gras
2352fe8fb19SBen Gras	if (p != NULL)
2362fe8fb19SBen Gras		free(p);
2372fe8fb19SBen Gras
2382fe8fb19SBen Gras	p = NULL;
2392fe8fb19SBen Gras	return NULL;
2402fe8fb19SBen Gras}
2412fe8fb19SBen Gras
2422fe8fb19SBen Grasp = p2;
2432fe8fb19SBen Grassize = newsize;
2442fe8fb19SBen Gras.Ed
2452fe8fb19SBen Gras.Sh SEE ALSO
2462fe8fb19SBen Gras.\" .Xr limits 1 ,
2472fe8fb19SBen Gras.Xr madvise 2 ,
2482fe8fb19SBen Gras.Xr mmap 2 ,
2492fe8fb19SBen Gras.Xr sbrk 2 ,
2502fe8fb19SBen Gras.Xr alloca 3 ,
2512fe8fb19SBen Gras.Xr atexit 3 ,
2522fe8fb19SBen Gras.Xr getpagesize 3 ,
2532fe8fb19SBen Gras.Xr memory 3 ,
254*0a6a1f1dSLionel Sambuc.Xr posix_memalign 3 ,
255*0a6a1f1dSLionel Sambuc.Xr reallocarr 3
2562fe8fb19SBen Gras.Pp
2572fe8fb19SBen GrasFor the implementation details, see
2582fe8fb19SBen Gras.Xr jemalloc 3 .
2592fe8fb19SBen Gras.Sh STANDARDS
2602fe8fb19SBen GrasThe
2612fe8fb19SBen Gras.Fn malloc ,
2622fe8fb19SBen Gras.Fn calloc ,
2632fe8fb19SBen Gras.Fn realloc
2642fe8fb19SBen Grasand
2652fe8fb19SBen Gras.Fn free
2662fe8fb19SBen Grasfunctions conform to
2672fe8fb19SBen Gras.St -isoC .
268*0a6a1f1dSLionel Sambuc.Sh HISTORY
269*0a6a1f1dSLionel SambucA
270*0a6a1f1dSLionel Sambuc.Fn free
271*0a6a1f1dSLionel Sambucinternal kernel function and a predecessor to
272*0a6a1f1dSLionel Sambuc.Fn malloc ,
273*0a6a1f1dSLionel Sambuc.Fn alloc ,
274*0a6a1f1dSLionel Sambucfirst appeared in
275*0a6a1f1dSLionel Sambuc.At v1 .
276*0a6a1f1dSLionel SambucThe C Library functions
277*0a6a1f1dSLionel Sambuc.Fn alloc
278*0a6a1f1dSLionel Sambucand
279*0a6a1f1dSLionel Sambuc.Fn free
280*0a6a1f1dSLionel Sambucappeared in
281*0a6a1f1dSLionel Sambuc.At v6 .
282*0a6a1f1dSLionel SambucThe functions
283*0a6a1f1dSLionel Sambuc.Fn malloc ,
284*0a6a1f1dSLionel Sambuc.Fn calloc ,
285*0a6a1f1dSLionel Sambucand
286*0a6a1f1dSLionel Sambuc.Fn realloc
287*0a6a1f1dSLionel Sambucfirst appeared in
288*0a6a1f1dSLionel Sambuc.At v7 .
289*0a6a1f1dSLionel Sambuc.Pp
290*0a6a1f1dSLionel SambucA new implementation by Chris Kingsley was introduced in
291*0a6a1f1dSLionel Sambuc.Bx 4.2 ,
292*0a6a1f1dSLionel Sambucfollowed by a complete rewrite by Poul-Henning Kamp
293*0a6a1f1dSLionel Sambuc.Dq ( phk's malloc
294*0a6a1f1dSLionel Sambucor
295*0a6a1f1dSLionel Sambuc.Dq new malloc )
296*0a6a1f1dSLionel Sambucwhich appeared in
297*0a6a1f1dSLionel Sambuc.Fx 2.2
298*0a6a1f1dSLionel Sambucand was included in
299*0a6a1f1dSLionel Sambuc.Nx 1.5
300*0a6a1f1dSLionel Sambucand
301*0a6a1f1dSLionel Sambuc.Ox 2.0 .
302*0a6a1f1dSLionel SambucThese implementations were all
303*0a6a1f1dSLionel Sambuc.Xr sbrk 2
304*0a6a1f1dSLionel Sambucbased.
305*0a6a1f1dSLionel Sambuc.Pp
306*0a6a1f1dSLionel SambucThe
307*0a6a1f1dSLionel Sambuc.Xr jemalloc 3
308*0a6a1f1dSLionel Sambucallocator became the default system allocator first in
309*0a6a1f1dSLionel Sambuc.Fx 7.0
310*0a6a1f1dSLionel Sambucand then in
311*0a6a1f1dSLionel Sambuc.Nx 5.0 .
312