version 1, including all changes.
.
Rev |
Author |
# |
Line |
1 |
perry |
1 |
MALLOC |
|
|
2 |
!!!MALLOC |
|
|
3 |
NAME |
|
|
4 |
SYNOPSIS |
|
|
5 |
DESCRIPTION |
|
|
6 |
RETURN VALUE |
|
|
7 |
CONFORMING TO |
|
|
8 |
SEE ALSO |
|
|
9 |
NOTES |
|
|
10 |
---- |
|
|
11 |
!!NAME |
|
|
12 |
|
|
|
13 |
|
|
|
14 |
calloc, malloc, free, realloc - Allocate and free dynamic memory |
|
|
15 |
!!SYNOPSIS |
|
|
16 |
|
|
|
17 |
|
|
|
18 |
__#include |
|
|
19 |
__ ''nmemb''__, size_t__ ''size''__); |
|
|
20 |
void *malloc(size_t__ ''size''__); |
|
|
21 |
void free(void__ ''*ptr''__); |
|
|
22 |
void *realloc(void__ ''*ptr''__, size_t__ ''size''__); |
|
|
23 |
__ |
|
|
24 |
!!DESCRIPTION |
|
|
25 |
|
|
|
26 |
|
|
|
27 |
__calloc()__ allocates memory for an array of |
|
|
28 |
''nmemb'' elements of ''size'' bytes each and returns |
|
|
29 |
a pointer to the allocated memory. The memory is set to |
|
|
30 |
zero. |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
__malloc()__ allocates ''size'' bytes and returns a |
|
|
34 |
pointer to the allocated memory. The memory is not |
|
|
35 |
cleared. |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
__free()__ frees the memory space pointed to by |
|
|
39 |
''ptr'', which must have been returned by a previous call |
|
|
40 |
to __malloc()__, __calloc()__ or __realloc()__. |
|
|
41 |
Otherwise, or if __free(__''ptr''__)__ has already |
|
|
42 |
been called before, undefined behaviour occurs. If |
|
|
43 |
''ptr'' is __NULL__, no operation is |
|
|
44 |
performed. |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
__realloc()__ changes the size of the memory block |
|
|
48 |
pointed to by ''ptr'' to ''size'' bytes. The contents |
|
|
49 |
will be unchanged to the minimum of the old and new sizes; |
|
|
50 |
newly allocated memory will be uninitialized. If ''ptr'' |
|
|
51 |
is __NULL__, the call is equivalent to |
|
|
52 |
__malloc(size)__; if size is equal to zero, the call is |
|
|
53 |
equivalent to __free(__''ptr''__)__''.'' Unless |
|
|
54 |
''ptr'' is __NULL__, it must have been returned by an |
|
|
55 |
earlier call to __malloc()__, __calloc()__ or |
|
|
56 |
__realloc()__. |
|
|
57 |
!!RETURN VALUE |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
For __calloc()__ and __malloc()__, the value returned |
|
|
61 |
is a pointer to the allocated memory, which is suitably |
|
|
62 |
aligned for any kind of variable, or __NULL__ if the |
|
|
63 |
request fails. |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
__free()__ returns no value. |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
__realloc()__ returns a pointer to the newly allocated |
|
|
70 |
memory, which is suitably aligned for any kind of variable |
|
|
71 |
and may be different from ''ptr'', or __NULL__ if the |
|
|
72 |
request fails or if size was equal to 0. If __realloc()__ |
|
|
73 |
fails the original block is left untouched - it is not freed |
|
|
74 |
or moved. |
|
|
75 |
!!CONFORMING TO |
|
|
76 |
|
|
|
77 |
|
|
|
78 |
ANSI-C |
|
|
79 |
!!SEE ALSO |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
brk(2) |
|
|
83 |
!!NOTES |
|
|
84 |
|
|
|
85 |
|
|
|
86 |
The Unix98 standard requires __malloc()__, |
|
|
87 |
__calloc()__, and __realloc__() to set ''errno'' to |
|
|
88 |
ENOMEM upon failure. Glibc assumes that this is done (and |
|
|
89 |
the glibc versions of these routines do this); if you use a |
|
|
90 |
private malloc implementation that does not set |
|
|
91 |
''errno'', then certain library routines may fail without |
|
|
92 |
having a reason in ''errno''. |
|
|
93 |
|
|
|
94 |
|
|
|
95 |
Crashes in __malloc()__, __free()__ or |
|
|
96 |
__realloc()__ are almost always related to heap |
|
|
97 |
corruption, such as overflowing an allocated chunk or |
|
|
98 |
freeing the same pointer twice. |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
Recent versions of Linux libc (later than 5.4.23) and GNU |
|
|
102 |
libc (2.x) include a malloc implementation which is tunable |
|
|
103 |
via environment variables. When __MALLOC_CHECK___ is set, |
|
|
104 |
a special (less efficient) implementation is used which is |
|
|
105 |
designed to be tolerant against simple errors, such as |
|
|
106 |
double calls of __free()__ with the same argument, or |
|
|
107 |
overruns of a single byte (off-by-one bugs). Not all such |
|
|
108 |
errors can be proteced against, however, and memory leaks |
|
|
109 |
can result. If __MALLOC_CHECK___ is set to 0, any |
|
|
110 |
detected heap corruption is silently ignored; if set to 1, a |
|
|
111 |
diagnostic is printed on stderr; if set to 2, __abort()__ |
|
|
112 |
is called immediately. This can be useful because otherwise |
|
|
113 |
a crash may happen much later, and the true cause for the |
|
|
114 |
problem is then very hard to track down. |
|
|
115 |
|
|
|
116 |
|
|
|
117 |
Linux follows an optimistic memory allocation strategy. This |
|
|
118 |
means that when __malloc()__ returns non-NULL there is no |
|
|
119 |
guarantee that the memory really is available. In case it |
|
|
120 |
turns out that the system is out of memory, one or more |
|
|
121 |
processes will be killed by the infamous OOM |
|
|
122 |
killer. |
|
|
123 |
---- |