summaryrefslogtreecommitdiffstats
path: root/man3/malloc.3
diff options
context:
space:
mode:
Diffstat (limited to 'man3/malloc.3')
-rw-r--r--man3/malloc.3111
1 files changed, 80 insertions, 31 deletions
diff --git a/man3/malloc.3 b/man3/malloc.3
index 71b776f86..eee0b3069 100644
--- a/man3/malloc.3
+++ b/man3/malloc.3
@@ -1,6 +1,6 @@
'\" t
.\" Copyright (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
-.\" and Copyright i2007, 2012, 2018, Michael Kerrisk <mtk.manpages@gmail.com>
+.\" and Copyright 2007, 2012, 2018, Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
@@ -12,7 +12,7 @@
.\" FIXME . Review http://austingroupbugs.net/view.php?id=374
.\" to see what changes are required on this page.
.\"
-.TH malloc 3 2023-02-05 "Linux man-pages 6.03"
+.TH malloc 3 2023-07-20 "Linux man-pages 6.05.01"
.SH NAME
malloc, free, calloc, realloc, reallocarray \- allocate and free dynamic memory
.SH LIBRARY
@@ -22,11 +22,11 @@ Standard C library
.nf
.B #include <stdlib.h>
.PP
-.BI "void *malloc(size_t " "size" );
-.BI "void free(void " "*ptr" );
-.BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
-.BI "void *realloc(void " "*ptr" ", size_t " "size" );
-.BI "void *reallocarray(void " "*ptr" ", size_t " nmemb ", size_t " "size" );
+.BI "void *malloc(size_t " size );
+.BI "void free(void *_Nullable " ptr );
+.BI "void *calloc(size_t " nmemb ", size_t " size );
+.BI "void *realloc(void *_Nullable " ptr ", size_t " size );
+.BI "void *reallocarray(void *_Nullable " ptr ", size_t " nmemb ", size_t " size );
.fi
.PP
.RS -4
@@ -226,48 +226,65 @@ or
.B RLIMIT_DATA
limit described in
.BR getrlimit (2).
-.SH VERSIONS
-.BR reallocarray ()
-was added in glibc 2.26.
-.PP
-.BR malloc ()
-and related functions rejected sizes greater than
-.B PTRDIFF_MAX
-starting in glibc 2.30.
-.PP
-.BR free ()
-preserved
-.I errno
-starting in glibc 2.33.
+Another reason could be that
+the number of mappings created by the caller process
+exceeded the limit specified by
+.IR /proc/sys/vm/max_map_count .
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
-.ad l
-.nh
.TS
allbox;
lbx lb lb
l l l.
Interface Attribute Value
T{
+.na
+.nh
.BR malloc (),
.BR free (),
.BR calloc (),
.BR realloc ()
T} Thread safety MT-Safe
.TE
-.hy
-.ad
.sp 1
.SH STANDARDS
-.BR malloc (),
-.BR free (),
-.BR calloc (),
-.BR realloc ():
-POSIX.1-2001, POSIX.1-2008, C99.
-.PP
+.TP
+.BR malloc ()
+.TQ
+.BR free ()
+.TQ
+.BR calloc ()
+.TQ
+.BR realloc ()
+C11, POSIX.1-2008.
+.TP
.BR reallocarray ()
-is a nonstandard extension that first appeared in OpenBSD 5.6 and FreeBSD 11.0.
+None.
+.SH HISTORY
+.TP
+.BR malloc ()
+.TQ
+.BR free ()
+.TQ
+.BR calloc ()
+.TQ
+.BR realloc ()
+POSIX.1-2001, C89.
+.TP
+.BR reallocarray ()
+glibc 2.26.
+OpenBSD 5.6, FreeBSD 11.0.
+.PP
+.BR malloc ()
+and related functions rejected sizes greater than
+.B PTRDIFF_MAX
+starting in glibc 2.30.
+.PP
+.BR free ()
+preserved
+.I errno
+starting in glibc 2.33.
.SH NOTES
By default, Linux follows an optimistic memory allocation strategy.
This means that when
@@ -381,6 +398,38 @@ as POSIX and the C standard do not allow replacement of
.BR calloc (),
and
.BR realloc ().
+.SH EXAMPLES
+.EX
+#include <err.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+\&
+#define MALLOCARRAY(n, type) ((type *) my_mallocarray(n, sizeof(type)))
+#define MALLOC(type) MALLOCARRAY(1, type)
+\&
+static inline void *my_mallocarray(size_t nmemb, size_t size);
+\&
+int
+main(void)
+{
+ char *p;
+\&
+ p = MALLOCARRAY(32, char);
+ if (p == NULL)
+ err(EXIT_FAILURE, "malloc");
+\&
+ strlcpy(p, "foo", 32);
+ puts(p);
+}
+\&
+static inline void *
+my_mallocarray(size_t nmemb, size_t size)
+{
+ return reallocarray(NULL, nmemb, size);
+}
+.EE
.SH SEE ALSO
.\" http://g.oswego.edu/dl/html/malloc.html
.\" A Memory Allocator - by Doug Lea