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