2004-11-03 13:51:07 +00:00
|
|
|
.\" (c) 1993 by Thomas Koenig (ig25@rz.uni-karlsruhe.de)
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
2007-04-12 22:42:49 +00:00
|
|
|
.\"
|
2004-11-03 13:51:07 +00:00
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\" License.
|
|
|
|
.\" Modified Sat Jul 24 19:00:59 1993 by Rik Faith (faith@cs.unc.edu)
|
|
|
|
.\" Clarification concerning realloc, iwj10@cus.cam.ac.uk (Ian Jackson), 950701
|
|
|
|
.\" Documented MALLOC_CHECK_, Wolfram Gloger (wmglo@dent.med.uni-muenchen.de)
|
|
|
|
.\"
|
|
|
|
.TH MALLOC 3 1993-04-04 "GNU" "Linux Programmer's Manual"
|
|
|
|
.SH NAME
|
|
|
|
calloc, malloc, free, realloc \- Allocate and free dynamic memory
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <stdlib.h>
|
|
|
|
.sp
|
|
|
|
.BI "void *calloc(size_t " "nmemb" ", size_t " "size" );
|
2006-07-20 16:16:51 +00:00
|
|
|
.br
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "void *malloc(size_t " "size" );
|
2006-07-20 16:16:51 +00:00
|
|
|
.br
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "void free(void " "*ptr" );
|
2006-07-20 16:16:51 +00:00
|
|
|
.br
|
2004-11-03 13:51:07 +00:00
|
|
|
.BI "void *realloc(void " "*ptr" ", size_t " "size" );
|
|
|
|
.fi
|
|
|
|
.SH DESCRIPTION
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR calloc ()
|
2007-04-12 22:42:49 +00:00
|
|
|
allocates memory for an array of
|
2004-11-03 13:51:07 +00:00
|
|
|
.I nmemb
|
2007-04-12 22:42:49 +00:00
|
|
|
elements of
|
2004-11-03 13:51:07 +00:00
|
|
|
.I size
|
2007-04-12 22:42:49 +00:00
|
|
|
bytes each and returns a pointer to the allocated memory.
|
2004-11-03 13:51:07 +00:00
|
|
|
The memory is set to zero.
|
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR malloc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
allocates
|
|
|
|
.I size
|
2007-04-12 22:42:49 +00:00
|
|
|
bytes and returns a pointer to the allocated memory.
|
2004-11-03 13:51:07 +00:00
|
|
|
The memory is not cleared.
|
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR free ()
|
2004-11-03 13:51:07 +00:00
|
|
|
frees the memory space pointed to by
|
|
|
|
.IR ptr ,
|
|
|
|
which must have been returned by a previous call to
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR malloc (),
|
|
|
|
.BR calloc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR realloc ().
|
2004-11-03 13:51:07 +00:00
|
|
|
Otherwise, or if
|
|
|
|
.BI "free(" "ptr" )
|
|
|
|
has already been called before, undefined behaviour occurs.
|
|
|
|
If
|
|
|
|
.I ptr
|
2005-11-02 13:55:25 +00:00
|
|
|
is NULL, no operation is performed.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR realloc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
changes the size of the memory block pointed to by
|
|
|
|
.I ptr
|
|
|
|
to
|
|
|
|
.I size
|
|
|
|
bytes.
|
|
|
|
The contents will be unchanged to the minimum of the old and new sizes;
|
|
|
|
newly allocated memory will be uninitialized.
|
|
|
|
If
|
|
|
|
.I ptr
|
2005-11-02 13:55:25 +00:00
|
|
|
is NULL, the call is equivalent to
|
2007-05-21 21:25:44 +00:00
|
|
|
.IR malloc(size) ;
|
2007-04-12 22:42:49 +00:00
|
|
|
if
|
|
|
|
.I size
|
2005-11-15 17:56:36 +00:00
|
|
|
is equal to zero,
|
2004-11-03 13:51:07 +00:00
|
|
|
the call is equivalent to
|
|
|
|
.BI "free(" "ptr" ) .
|
|
|
|
Unless
|
|
|
|
.I ptr
|
2005-11-02 13:55:25 +00:00
|
|
|
is NULL, it must have been returned by an earlier call to
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR malloc (),
|
|
|
|
.BR calloc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR realloc ().
|
2004-11-03 13:51:07 +00:00
|
|
|
If the area pointed to was moved, a
|
|
|
|
.BI "free(" "ptr" )
|
|
|
|
is done.
|
|
|
|
.SH "RETURN VALUE"
|
|
|
|
For
|
2007-04-12 22:42:49 +00:00
|
|
|
.BR calloc ()
|
|
|
|
and
|
2005-10-19 07:29:28 +00:00
|
|
|
.BR malloc (),
|
2004-11-03 13:51:07 +00:00
|
|
|
the value returned is a pointer to the allocated memory, which is suitably
|
2005-11-02 13:55:25 +00:00
|
|
|
aligned for any kind of variable, or NULL if the request fails.
|
2004-11-03 13:51:07 +00:00
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR free ()
|
2004-11-03 13:51:07 +00:00
|
|
|
returns no value.
|
|
|
|
.PP
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR realloc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
returns a pointer to the newly allocated memory, which is suitably
|
|
|
|
aligned for any kind of variable and may be different from
|
|
|
|
.IR ptr ,
|
2007-04-12 22:42:49 +00:00
|
|
|
or NULL if the request fails.
|
2005-11-02 13:55:25 +00:00
|
|
|
If
|
2004-11-03 13:51:07 +00:00
|
|
|
.I size
|
|
|
|
was equal to 0, either NULL or a pointer suitable to be passed to
|
2005-11-02 11:34:24 +00:00
|
|
|
.BR free ()
|
2007-04-12 22:42:49 +00:00
|
|
|
is returned.
|
|
|
|
If
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR realloc ()
|
2005-07-06 08:00:30 +00:00
|
|
|
fails the original block is left untouched; it is not freed or moved.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "CONFORMING TO"
|
2006-08-03 13:57:30 +00:00
|
|
|
C89, C99.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NOTES
|
|
|
|
The Unix98 standard requires
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR malloc (),
|
|
|
|
.BR calloc (),
|
2004-11-03 13:51:07 +00:00
|
|
|
and
|
|
|
|
.BR realloc ()
|
|
|
|
to set
|
|
|
|
.I errno
|
2007-04-12 22:42:49 +00:00
|
|
|
to ENOMEM upon failure.
|
|
|
|
Glibc assumes that this is done
|
2004-11-03 13:51:07 +00:00
|
|
|
(and the glibc versions of these routines do this); if you
|
|
|
|
use a private malloc implementation that does not set
|
|
|
|
.IR errno ,
|
|
|
|
then certain library routines may fail without having
|
|
|
|
a reason in
|
|
|
|
.IR errno .
|
|
|
|
.LP
|
|
|
|
Crashes in
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR malloc (),
|
2006-12-27 05:28:31 +00:00
|
|
|
.BR calloc (),
|
|
|
|
.BR realloc (),
|
2004-11-03 13:51:07 +00:00
|
|
|
or
|
2006-12-27 05:28:31 +00:00
|
|
|
.BR free ()
|
2004-11-03 13:51:07 +00:00
|
|
|
are almost always related to heap corruption, such as overflowing
|
|
|
|
an allocated chunk or freeing the same pointer twice.
|
|
|
|
.PP
|
|
|
|
Recent versions of Linux libc (later than 5.4.23) and GNU libc (2.x)
|
|
|
|
include a malloc implementation which is tunable via environment
|
2007-04-12 22:42:49 +00:00
|
|
|
variables.
|
|
|
|
When
|
2004-11-03 13:51:07 +00:00
|
|
|
.BR MALLOC_CHECK_
|
|
|
|
is set, a special (less efficient) implementation is used which
|
|
|
|
is designed to be tolerant against simple errors, such as double
|
|
|
|
calls of
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR free ()
|
2004-11-03 13:51:07 +00:00
|
|
|
with the same argument, or overruns of a single byte (off-by-one
|
2007-04-12 22:42:49 +00:00
|
|
|
bugs).
|
|
|
|
Not all such errors can be protected against, however, and
|
2004-11-03 13:51:07 +00:00
|
|
|
memory leaks can result.
|
|
|
|
If
|
|
|
|
.BR MALLOC_CHECK_
|
|
|
|
is set to 0, any detected heap corruption is silently ignored;
|
|
|
|
if set to 1, a diagnostic is printed on stderr;
|
|
|
|
if set to 2,
|
2007-05-12 00:30:29 +00:00
|
|
|
.BR abort (3)
|
2007-04-12 22:42:49 +00:00
|
|
|
is called immediately.
|
|
|
|
This can be useful because otherwise
|
2004-11-03 13:51:07 +00:00
|
|
|
a crash may happen much later, and the true cause for the problem
|
|
|
|
is then very hard to track down.
|
|
|
|
.SH BUGS
|
|
|
|
By default, Linux follows an optimistic memory allocation strategy.
|
|
|
|
This means that when
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR malloc ()
|
2004-11-03 13:51:07 +00:00
|
|
|
returns non-NULL there is no guarantee that the memory really
|
2007-04-12 22:42:49 +00:00
|
|
|
is available.
|
|
|
|
This is a really bad bug.
|
2004-11-03 13:51:07 +00:00
|
|
|
In case it turns out that the system is out of memory,
|
|
|
|
one or more processes will be killed by the infamous OOM killer.
|
|
|
|
In case Linux is employed under circumstances where it would be
|
|
|
|
less desirable to suddenly lose some randomly picked processes,
|
|
|
|
and moreover the kernel version is sufficiently recent,
|
|
|
|
one can switch off this overcommitting behavior using a command like
|
|
|
|
.RS
|
|
|
|
# echo 2 > /proc/sys/vm/overcommit_memory
|
|
|
|
.RE
|
|
|
|
See also the kernel Documentation directory, files
|
|
|
|
.I vm/overcommit-accounting
|
|
|
|
and
|
|
|
|
.IR sysctl/vm.txt .
|
2007-05-16 18:25:50 +00:00
|
|
|
.SH "SEE ALSO"
|
|
|
|
.BR brk (2),
|
|
|
|
.BR posix_memalign (3)
|