man-pages/man3/malloc.3.html
2021-03-31 01:06:50 +01:00

576 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of MALLOC</TITLE>
</HEAD><BODY>
<H1>MALLOC</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2020-02-09<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
malloc, free, calloc, realloc - allocate and free dynamic memory
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>void *malloc(size_t </B><I>size</I><B>);</B>
<B>void free(void </B><I>*ptr</I><B>);</B>
<B>void *calloc(size_t </B><I>nmemb</I><B>, size_t </B><I>size</I><B>);</B>
<B>void *realloc(void </B><I>*ptr</I><B>, size_t </B><I>size</I><B>);</B>
<B>void *reallocarray(void </B><I>*ptr</I><B>, size_t </B><I>nmemb</I><B>, size_t </B><I>size</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>reallocarray</B>():
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Since&nbsp;glibc&nbsp;2.29:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_DEFAULT_SOURCE
<BR>&nbsp;&nbsp;&nbsp;&nbsp;Glibc&nbsp;2.28&nbsp;and&nbsp;earlier:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_GNU_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
<P>
The
<B>malloc</B>()
function allocates
<I>size</I>
bytes and returns a pointer to the allocated memory.
<I>The memory is not initialized</I>.
If
<I>size</I>
is 0, then
<B>malloc</B>()
returns either NULL,
or a unique pointer value that can later be successfully passed to
<B>free</B>().
<P>
The
<B>free</B>()
function frees the memory space pointed to by
<I>ptr</I>,
which must have been returned by a previous call to
<B>malloc</B>(),
<B>calloc</B>(),
or
<B>realloc</B>().
Otherwise, or if
<I>free(ptr)</I>
has already been called before, undefined behavior occurs.
If
<I>ptr</I>
is NULL, no operation is performed.
<P>
The
<B>calloc</B>()
function allocates memory for an array of
<I>nmemb</I>
elements of
<I>size</I>
bytes each and returns a pointer to the allocated memory.
The memory is set to zero.
If
<I>nmemb</I>
or
<I>size</I>
is 0, then
<B>calloc</B>()
returns either NULL,
or a unique pointer value that can later be successfully passed to
<B>free</B>().
If the multiplication of
<I>nmemb</I>
and
<I>size</I>
would result in integer overflow, then
<B>calloc</B>()
returns an error.
By contrast,
an integer overflow would not be detected in the following call to
<B>malloc</B>(),
with the result that an incorrectly sized block of memory would be allocated:
<P>
malloc(nmemb * size);
<P>
The
<B>realloc</B>()
function changes the size of the memory block pointed to by
<I>ptr</I>
to
<I>size</I>
bytes.
The contents will be unchanged in the range from the start of the region
up to the minimum of the old and new sizes.
If the new size is larger than the old size, the added memory will
<I>not</I>
be initialized.
If
<I>ptr</I>
is NULL, then the call is equivalent to
<I>malloc(size)</I>,
for all values of
<I>size</I>;
if
<I>size</I>
is equal to zero,
and
<I>ptr</I>
is not NULL, then the call is equivalent to
<I>free(ptr)</I>.
Unless
<I>ptr</I>
is NULL, it must have been returned by an earlier call to
<B>malloc</B>(),
<B>calloc</B>(),
or
<B>realloc</B>().
If the area pointed to was moved, a
<I>free(ptr)</I>
is done.
<P>
The
<B>reallocarray</B>()
function changes the size of the memory block pointed to by
<I>ptr</I>
to be large enough for an array of
<I>nmemb</I>
elements, each of which is
<I>size</I>
bytes.
It is equivalent to the call
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;realloc(ptr,&nbsp;nmemb&nbsp;*&nbsp;size);
<P>
However, unlike that
<B>realloc</B>()
call,
<B>reallocarray</B>()
fails safely in the case where the multiplication would overflow.
If such an overflow occurs,
<B>reallocarray</B>()
returns NULL, sets
<I>errno</I>
to
<B>ENOMEM</B>,
and leaves the original block of memory unchanged.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>malloc</B>()
and
<B>calloc</B>()
functions return a pointer to the allocated memory,
which is suitably aligned for any built-in type.
On error, these functions return NULL.
NULL may also be returned by a successful call to
<B>malloc</B>()
with a
<I>size</I>
of zero,
or by a successful call to
<B>calloc</B>()
with
<I>nmemb</I>
or
<I>size</I>
equal to zero.
<P>
The
<B>free</B>()
function returns no value.
<P>
The
<B>realloc</B>()
function returns a pointer to the newly allocated memory, which is suitably
aligned for any built-in type, or NULL if the request failed.
The returned pointer may be the same as
<I>ptr</I>
if the allocation was not moved
(e.g., there was room to expand the allocation in-place), or different from
<I>ptr</I>
if the allocation was moved to a new address.
If
<I>size</I>
was equal to 0, either NULL or a pointer suitable to be passed to
<B>free</B>()
is returned.
If
<B>realloc</B>()
fails, the original block is left untouched; it is not freed or moved.
<P>
On success, the
<B>reallocarray</B>()
function returns a pointer to the newly allocated memory.
On failure,
it returns NULL and the original block of memory is left untouched.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<B>calloc</B>(),
<B>malloc</B>(),
<B>realloc</B>(),
and
<B>reallocarray</B>()
can fail with the following error:
<DL COMPACT>
<DT id="1"><B>ENOMEM</B>
<DD>
Out of memory.
Possibly, the application hit the
<B>RLIMIT_AS</B>
or
<B>RLIMIT_DATA</B>
limit described in
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2).
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>malloc</B>(),
<B>free</B>(),
<BR>
<B>calloc</B>(),
<B>realloc</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>malloc</B>(),
<B>free</B>(),
<B>calloc</B>(),
<B>realloc</B>():
POSIX.1-2001, POSIX.1-2008, C89, C99.
<P>
<B>reallocarray</B>()
is a nonstandard extension that first appeared in OpenBSD 5.6 and FreeBSD 11.0.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
By default, Linux follows an optimistic memory allocation strategy.
This means that when
<B>malloc</B>()
returns non-NULL there is no guarantee that the memory really
is available.
In case it turns out that the system is out of memory,
one or more processes will be killed by the OOM killer.
For more information, see the description of
<I>/proc/sys/vm/overcommit_memory</I>
and
<I>/proc/sys/vm/oom_adj</I>
in
<B><A HREF="/cgi-bin/man/man2html?5+proc">proc</A></B>(5),
and the Linux kernel source file
<I>Documentation/vm/overcommit-accounting.rst</I>.
<P>
Normally,
<B>malloc</B>()
allocates memory from the heap, and adjusts the size of the heap
as required, using
<B><A HREF="/cgi-bin/man/man2html?2+sbrk">sbrk</A></B>(2).
When allocating blocks of memory larger than
<B>MMAP_THRESHOLD</B>
bytes, the glibc
<B>malloc</B>()
implementation allocates the memory as a private anonymous mapping using
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2).
<B>MMAP_THRESHOLD</B>
is 128&nbsp;kB by default, but is adjustable using
<B><A HREF="/cgi-bin/man/man2html?3+mallopt">mallopt</A></B>(3).
Prior to Linux 4.7
allocations performed using
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)
were unaffected by the
<B>RLIMIT_DATA</B>
resource limit;
since Linux 4.7, this limit is also enforced for allocations performed using
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2).
<P>
To avoid corruption in multithreaded applications,
mutexes are used internally to protect the memory-management
data structures employed by these functions.
In a multithreaded application in which threads simultaneously
allocate and free memory,
there could be contention for these mutexes.
To scalably handle memory allocation in multithreaded applications,
glibc creates additional
<I>memory allocation arenas</I>
if mutex contention is detected.
Each arena is a large region of memory that is internally allocated
by the system
(using
<B><A HREF="/cgi-bin/man/man2html?2+brk">brk</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2)),
and managed with its own mutexes.
<P>
SUSv2 requires
<B>malloc</B>(),
<B>calloc</B>(),
and
<B>realloc</B>()
to set
<I>errno</I>
to
<B>ENOMEM</B>
upon failure.
Glibc assumes that this is done
(and the glibc versions of these routines do this); if you
use a private malloc implementation that does not set
<I>errno</I>,
then certain library routines may fail without having
a reason in
<I>errno</I>.
<P>
Crashes in
<B>malloc</B>(),
<B>calloc</B>(),
<B>realloc</B>(),
or
<B>free</B>()
are almost always related to heap corruption, such as overflowing
an allocated chunk or freeing the same pointer twice.
<P>
The
<B>malloc</B>()
implementation is tunable via environment variables; see
<B><A HREF="/cgi-bin/man/man2html?3+mallopt">mallopt</A></B>(3)
for details.
<A NAME="lbAJ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+valgrind">valgrind</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+brk">brk</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+mmap">mmap</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+alloca">alloca</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc_get_state">malloc_get_state</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc_info">malloc_info</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc_trim">malloc_trim</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc_usable_size">malloc_usable_size</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mallopt">mallopt</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mcheck">mcheck</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+mtrace">mtrace</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+posix_memalign">posix_memalign</A></B>(3)
<P>
For details of the GNU C library implementation, see
<A NAME="lbAK">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="5"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="6"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="7"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="8"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="9"><A HREF="#lbAI">NOTES</A><DD>
<DT id="10"><A HREF="#lbAJ">SEE ALSO</A><DD>
<DT id="11"><A HREF="#lbAK">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:47 GMT, March 31, 2021
</BODY>
</HTML>