1MALLOC(3)                  Linux Programmer's Manual                 MALLOC(3)
2
3
4

NAME

6       malloc, free, calloc, realloc - allocate and free dynamic memory
7

SYNOPSIS

9       #include <stdlib.h>
10
11       void *malloc(size_t size);
12       void free(void *ptr);
13       void *calloc(size_t nmemb, size_t size);
14       void *realloc(void *ptr, size_t size);
15

DESCRIPTION

17       The malloc() function allocates size bytes and returns a pointer to the
18       allocated memory.  The memory is not initialized.  If size is  0,  then
19       malloc()  returns either NULL, or a unique pointer value that can later
20       be successfully passed to free().
21
22       The free() function frees the memory space pointed  to  by  ptr,  which
23       must  have  been  returned  by a previous call to malloc(), calloc() or
24       realloc().  Otherwise, or if free(ptr) has already been called  before,
25       undefined behavior occurs.  If ptr is NULL, no operation is performed.
26
27       The  calloc()  function allocates memory for an array of nmemb elements
28       of size bytes each and returns a pointer to the allocated memory.   The
29       memory  is  set  to zero.  If nmemb or size is 0, then calloc() returns
30       either NULL, or a unique pointer value that can later  be  successfully
31       passed to free().
32
33       The  realloc() function changes the size of the memory block pointed to
34       by ptr to size bytes.  The contents will be unchanged in the range from
35       the start of the region up to the minimum of the old and new sizes.  If
36       the new size is larger than the old size, the added memory will not  be
37       initialized.   If  ptr  is  NULL,  then  the call is equivalent to mal‐
38       loc(size), for all values of size; if size is equal to zero, and ptr is
39       not  NULL,  then  the  call  is equivalent to free(ptr).  Unless ptr is
40       NULL, it must have been returned by an earlier call to  malloc(),  cal‐
41       loc()  or  realloc().  If the area pointed to was moved, a free(ptr) is
42       done.
43

RETURN VALUE

45       The malloc() and calloc() functions return a pointer to  the  allocated
46       memory  that  is  suitably aligned for any kind of variable.  On error,
47       these functions return NULL.  NULL may also be returned by a successful
48       call  to  malloc() with a size of zero, or by a successful call to cal‐
49       loc() with nmemb or size equal to zero.
50
51       The free() function returns no value.
52
53       The realloc() function returns a pointer to the newly allocated memory,
54       which is suitably aligned for any kind of variable and may be different
55       from ptr, or NULL if the request fails.  If size was equal to 0, either
56       NULL  or  a  pointer  suitable  to be passed to free() is returned.  If
57       realloc() fails the original block is left untouched; it is  not  freed
58       or moved.
59

CONFORMING TO

61       C89, C99.
62

NOTES

64       By  default,  Linux  follows  an optimistic memory allocation strategy.
65       This means that when malloc() returns non-NULL there  is  no  guarantee
66       that  the  memory  really  is available.  In case it turns out that the
67       system is out of memory, one or more processes will be  killed  by  the
68       OOM   killer.    For   more   information,   see   the  description  of
69       /proc/sys/vm/overcommit_memory and /proc/sys/vm/oom_adj in proc(5), and
70       the Linux kernel source file Documentation/vm/overcommit-accounting.
71
72       Normally, malloc() allocates memory from the heap, and adjusts the size
73       of the heap as required, using sbrk(2).  When allocating blocks of mem‐
74       ory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation
75       allocates the memory as a  private  anonymous  mapping  using  mmap(2).
76       MMAP_THRESHOLD  is  128  kB  by  default,  but is adjustable using mal‐
77       lopt(3).  Allocations performed using mmap(2)  are  unaffected  by  the
78       RLIMIT_DATA resource limit (see getrlimit(2)).
79
80       To  avoid  corruption  in  multithreaded applications, mutexes are used
81       internally to protect the memory-management data structures employed by
82       these  functions.   In  a  multithreaded  application  in which threads
83       simultaneously allocate and free memory, there could be contention  for
84       these  mutexes.   To scalably handle memory allocation in multithreaded
85       applications, glibc creates  additional  memory  allocation  arenas  if
86       mutex  contention  is detected.  Each arena is a large region of memory
87       that is internally allocated by the system (using brk(2)  or  mmap(2)),
88       and managed with its own mutexes.
89
90       The  UNIX 98 standard requires malloc(), calloc(), and realloc() to set
91       errno to ENOMEM upon failure.  Glibc assumes that this is done (and the
92       glibc  versions of these routines do this); if you use a private malloc
93       implementation that does not set errno, then certain  library  routines
94       may fail without having a reason in errno.
95
96       Crashes  in  malloc(), calloc(), realloc(), or free() are almost always
97       related to heap corruption, such as overflowing an allocated  chunk  or
98       freeing the same pointer twice.
99
100       Recent  versions  of  Linux  libc  (later  than 5.4.23) and glibc (2.x)
101       include a malloc() implementation  which  is  tunable  via  environment
102       variables.  For details, see mallopt(3).
103

SEE ALSO

105       brk(2), mmap(2), alloca(3), malloc_get_state(3), malloc_info(3),
106       malloc_trim(3), malloc_usable_size(3), mallopt(3), mcheck(3),
107       mtrace(3), posix_memalign(3)
108

COLOPHON

110       This page is part of release 3.53 of the Linux man-pages project.  A
111       description of the project, and information about reporting bugs, can
112       be found at http://www.kernel.org/doc/man-pages/.
113
114
115
116GNU                               2012-05-10                         MALLOC(3)
Impressum