1malloc(3C) Standard C Library Functions malloc(3C)
2
3
4
6 malloc, calloc, free, memalign, realloc, valloc, alloca - memory allo‐
7 cator
8
10 #include <stdlib.h>
11
12 void *malloc(size_t size);
13
14
15 void *calloc(size_t nelem, size_t elsize);
16
17
18 void free(void *ptr);
19
20
21 void *memalign(size_t alignment, size_t size);
22
23
24 void *realloc(void *ptr, size_t size);
25
26
27 void *valloc(size_t size);
28
29
30 #include <alloca.h>
31
32 void *alloca(size_t size);
33
34
36 The malloc() and free() functions provide a simple, general-purpose
37 memory allocation package. The malloc() function returns a pointer to a
38 block of at least size bytes suitably aligned for any use. If the space
39 assigned by malloc() is overrun, the results are undefined.
40
41
42 The argument to free() is a pointer to a block previously allocated by
43 malloc(), calloc(), or realloc(). After free() is executed, this space
44 is made available for further allocation by the application, though not
45 returned to the system. Memory is returned to the system only upon ter‐
46 mination of the application. If ptr is a null pointer, no action
47 occurs. If a random number is passed to free(), the results are unde‐
48 fined.
49
50
51 The calloc() function allocates space for an array of nelem elements of
52 size elsize. The space is initialized to zeros.
53
54
55 The memalign() function allocates size bytes on a specified alignment
56 boundary and returns a pointer to the allocated block. The value of the
57 returned address is guaranteed to be an even multiple of alignment. The
58 value of alignment must be a power of two and must be greater than or
59 equal to the size of a word.
60
61
62 The realloc() function changes the size of the block pointed to by ptr
63 to size bytes and returns a pointer to the (possibly moved) block. The
64 contents will be unchanged up to the lesser of the new and old sizes.
65 If the new size of the block requires movement of the block, the space
66 for the previous instantiation of the block is freed. If the new size
67 is larger, the contents of the newly allocated portion of the block are
68 unspecified. If ptr is NULL, realloc() behaves like malloc() for the
69 specified size. If size is 0 and ptr is not a null pointer, the space
70 pointed to is freed.
71
72
73 The valloc() function has the same effect as malloc(), except that the
74 allocated memory will be aligned to a multiple of the value returned by
75 sysconf(_SC_PAGESIZE).
76
77
78 The alloca() function allocates size bytes of space in the stack frame
79 of the caller, and returns a pointer to the allocated block. This tem‐
80 porary space is automatically freed when the caller returns. If the
81 allocated block is beyond the current stack limit, the resulting behav‐
82 ior is undefined.
83
85 Upon successful completion, each of the allocation functions returns a
86 pointer to space suitably aligned (after possible pointer coercion) for
87 storage of any type of object.
88
89
90 If there is no available memory, malloc(), realloc(), memalign(), val‐
91 loc(), and calloc() return a null pointer. When realloc() is called
92 with size > 0 and returns NULL, the block pointed to by ptr is left
93 intact. If size, nelem, or elsize is 0, either a null pointer or a
94 unique pointer that can be passed to free() is returned.
95
96
97 If malloc(), calloc(), or realloc() returns unsuccessfully, errno will
98 be set to indicate the error. The free() function does not set errno.
99
101 The malloc(), calloc(), and realloc() functions will fail if:
102
103 ENOMEM The physical limits of the system are exceeded by size bytes
104 of memory which cannot be allocated.
105
106
107 EAGAIN There is not enough memory available to allocate size bytes
108 of memory; but the application could try again later.
109
110
112 Portable applications should avoid using valloc() but should instead
113 use malloc() or mmap(2). On systems with a large page size, the number
114 of successful valloc() operations might be 0.
115
116
117 These default memory allocation routines are safe for use in multi‐
118 threaded applications but are not scalable. Concurrent accesses by mul‐
119 tiple threads are single-threaded through the use of a single lock.
120 Multithreaded applications that make heavy use of dynamic memory allo‐
121 cation should be linked with allocation libraries designed for concur‐
122 rent access, such as libumem(3LIB) or libmtmalloc(3LIB). Applications
123 that want to avoid using heap allocations (with brk(2)) can do so by
124 using either libumem or libmapmalloc(3LIB). The allocation libraries
125 libmalloc(3LIB) and libbsdmalloc(3LIB) are available for special needs.
126
127
128 Comparative features of the various allocation libraries can be found
129 in the umem_alloc(3MALLOC) manual page.
130
132 See attributes(5) for descriptions of the following attributes:
133
134
135
136
137 ┌─────────────────────────────┬─────────────────────────────┐
138 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
139 ├─────────────────────────────┼─────────────────────────────┤
140 │Interface Stability │See below. │
141 ├─────────────────────────────┼─────────────────────────────┤
142 │MT-Level │Safe │
143 └─────────────────────────────┴─────────────────────────────┘
144
145
146 The malloc(), calloc(), free(), realloc(), valloc() functions are Stan‐
147 dard. The memalign() and alloca() functions are Stable.
148
150 brk(2), getrlimit(2), libbsdmalloc(3LIB), libmalloc(3LIB), libmapmal‐
151 loc(3LIB), libmtmalloc(3LIB), libumem(3LIB), umem_alloc(3MALLOC),
152 watchmalloc(3MALLOC), attributes(5)
153
155 Undefined results will occur if the size requested for a block of mem‐
156 ory exceeds the maximum size of a process's heap, which can be obtained
157 with getrlimit(2)
158
159
160 The alloca() function is machine-, compiler-, and most of all, system-
161 dependent. Its use is strongly discouraged.
162
163
164
165SunOS 5.11 21 Mar 2005 malloc(3C)