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

NAME

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

SYNOPSIS

10       #include <stdlib.h>
11
12       void *malloc(size_t size);
13       void free(void *ptr);
14       void *calloc(size_t nmemb, size_t size);
15       void *realloc(void *ptr, size_t size);
16       void *reallocarray(void *ptr, size_t nmemb, size_t size);
17
18   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20       reallocarray():
21           Since glibc 2.29:
22               _DEFAULT_SOURCE
23           Glibc 2.28 and earlier:
24               _GNU_SOURCE
25

DESCRIPTION

27       The malloc() function allocates size bytes and returns a pointer to the
28       allocated  memory.   The memory is not initialized.  If size is 0, then
29       malloc() returns either NULL, or a unique pointer value that can  later
30       be successfully passed to free().
31
32       The  free()  function  frees  the memory space pointed to by ptr, which
33       must have been returned by a previous call to  malloc(),  calloc(),  or
34       realloc().   Otherwise, or if free(ptr) has already been called before,
35       undefined behavior occurs.  If ptr is NULL, no operation is performed.
36
37       The calloc() function allocates memory for an array of  nmemb  elements
38       of  size bytes each and returns a pointer to the allocated memory.  The
39       memory is set to zero.  If nmemb or size is 0,  then  calloc()  returns
40       either  NULL,  or a unique pointer value that can later be successfully
41       passed to free().  If the multiplication of nmemb and size would result
42       in  integer  overflow, then calloc() returns an error.  By contrast, an
43       integer overflow would not be detected in the following  call  to  mal‐
44       loc(),  with the result that an incorrectly sized block of memory would
45       be allocated:
46
47           malloc(nmemb * size);
48
49       The realloc() function changes the size of the memory block pointed  to
50       by ptr to size bytes.  The contents will be unchanged in the range from
51       the start of the region up to the minimum of the old and new sizes.  If
52       the  new size is larger than the old size, the added memory will not be
53       initialized.  If ptr is NULL, then  the  call  is  equivalent  to  mal‐
54       loc(size), for all values of size; if size is equal to zero, and ptr is
55       not NULL, then the call is equivalent to free(ptr)  (this  behavior  is
56       nonportable;  see  NOTES).   Unless  ptr is NULL, it must have been re‐
57       turned by an earlier call to malloc(), calloc(), or realloc().  If  the
58       area pointed to was moved, a free(ptr) is done.
59
60       The  reallocarray()  function  changes  the  size  of  the memory block
61       pointed to by ptr to be large enough for an array  of  nmemb  elements,
62       each of which is size bytes.  It is equivalent to the call
63
64               realloc(ptr, nmemb * size);
65
66       However, unlike that realloc() call, reallocarray() fails safely in the
67       case where the multiplication would overflow.  If such an overflow  oc‐
68       curs, reallocarray() returns NULL, sets