]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/malloc.3
ldd.1, sprof.1, accept.2, alarm.2, bind.2, chdir.2, clock_nanosleep.2, close.2, conne...
[thirdparty/man-pages.git] / man3 / malloc.3
CommitLineData
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 35malloc, 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 47The
63aa9df0 48.BR malloc ()
12ec54b4 49function allocates
fea681da 50.I size
c13182ef 51bytes and returns a pointer to the allocated memory.
655d270c 52.IR "The memory is not initialized" .
840b4581
MK
53If
54.I size
bf1c0ede 55is 0, then
840b4581
MK
56.BR malloc ()
57returns either NULL,
58.\" glibc does this:
59or a unique pointer value that can later be successfully passed to
60.BR free ().
fea681da 61.PP
12ec54b4 62The
63aa9df0 63.BR free ()
12ec54b4 64function frees the memory space pointed to by
fea681da
MK
65.IR ptr ,
66which must have been returned by a previous call to
63aa9df0 67.BR malloc (),
c8624d68 68.BR calloc (),
fea681da 69or
63aa9df0 70.BR realloc ().
fea681da 71Otherwise, or if
0daa9e92 72.I free(ptr)
d9bfdb9c 73has already been called before, undefined behavior occurs.
fea681da
MK
74If
75.I ptr
8478ee02 76is NULL, no operation is performed.
fea681da 77.PP
12ec54b4
MK
78The
79.BR calloc ()
80function allocates memory for an array of
81.I nmemb
82elements of
83.I size
84bytes each and returns a pointer to the allocated memory.
85The memory is set to zero.
86If
87.I nmemb
88or
89.I size
90is 0, then
91.BR calloc ()
92returns either NULL,
93.\" glibc does this:
94or a unique pointer value that can later be successfully passed to
95.BR free ().
96.PP
97The
63aa9df0 98.BR realloc ()
12ec54b4 99function changes the size of the memory block pointed to by
fea681da
MK
100.I ptr
101to
102.I size
103bytes.
b4b57a95 104The contents will be unchanged in the range from the start of the region
655d270c
MK
105up to the minimum of the old and new sizes.
106If the new size is larger than the old size, the added memory will
107.I not
108be initialized.
fea681da
MK
109If
110.I ptr
c4acc689 111is NULL, then the call is equivalent to
865c9fd8
MK
112.IR malloc(size) ,
113for all values of
114.IR size ;
c13182ef
MK
115if
116.I size
054fccc0 117is equal to zero,
c4acc689
MK
118and
119.I ptr
120is not NULL, then the call is equivalent to
840b4581 121.IR free(ptr) .
fea681da
MK
122Unless
123.I ptr
8478ee02 124is NULL, it must have been returned by an earlier call to
63aa9df0
MK
125.BR malloc (),
126.BR calloc ()
fea681da 127or
63aa9df0 128.BR realloc ().
fea681da 129If the area pointed to was moved, a
0daa9e92 130.I free(ptr)
fea681da 131is done.
47297adb 132.SH RETURN VALUE
12ec54b4
MK
133The
134.BR malloc ()
c13182ef 135and
12ec54b4 136.BR calloc ()
25630b27
GP
137functions return a pointer to the allocated memory,
138which is suitably aligned for any built-in type.
fdc6b831
MK
139On error, these functions return NULL.
140NULL may also be returned by a successful call to
141.BR malloc ()
142with a
143.I size
144of zero,
145or by a successful call to