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       void *reallocarray(void *ptr, size_t nmemb, size_t size);
16
17   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19       reallocarray():
20           _GNU_SOURCE
21

DESCRIPTION

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

RETURN VALUE

62       The  malloc()  and calloc() functions return a pointer to the allocated
63       memory, which is suitably aligned for any  built-in  type.   On  error,
64       these functions return NULL.  NULL may also be returned by a successful
65       call to malloc() with a size of zero, or by a successful call  to  cal‐
66       loc() with nmemb or size equal to zero.
67
68       The free() function returns no value.
69
70       The realloc() function returns a pointer to the newly allocated memory,
71       which is suitably aligned for any built-in type and  may  be  different