1
0

strinput3 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. MALLOC(3) Linux Programmer's Manual MALLOC(3)
  2. NAME
  3. malloc, free, calloc, realloc - Allocate and free dynamic memory
  4. SYNOPSIS
  5. #include <stdlib.h>
  6. void *malloc(size_t size);
  7. void free(void *ptr);
  8. void *calloc(size_t nmemb, size_t size);
  9. void *realloc(void *ptr, size_t size);
  10. DESCRIPTION
  11. The malloc() function allocates size bytes and returns a pointer to the
  12. allocated memory. The memory is not initialized. If size is 0, then
  13. malloc() returns either NULL, or a unique pointer value that can later
  14. be successfully passed to free().
  15. The free() function frees the memory space pointed to by ptr, which
  16. must have been returned by a previous call to malloc(), calloc() or
  17. realloc(). Otherwise, or if free(ptr) has already been called before,
  18. undefined behavior occurs. If ptr is NULL, no operation is performed.
  19. The calloc() function allocates memory for an array of nmemb elements
  20. of size bytes each and returns a pointer to the allocated memory. The
  21. memory is set to zero. If nmemb or size is 0, then calloc() returns
  22. either NULL, or a unique pointer value that can later be successfully
  23. passed to free().
  24. The realloc() function changes the size of the memory block pointed to
  25. by ptr to size bytes. The contents will be unchanged in the range from
  26. the start of the region up to the minimum of the old and new sizes. If
  27. the new size is larger than the old size, the added memory will not be
  28. initialized. If ptr is NULL, then the call is equivalent to mal‐
  29. loc(size), for all values of size; if size is equal to zero, and ptr is
  30. not NULL, then the call is equivalent to free(ptr). Unless ptr is
  31. NULL, it must have been returned by an earlier call to malloc(), cal‐
  32. loc() or realloc(). If the area pointed to was moved, a free(ptr) is
  33. done.
  34. RETURN VALUE
  35. The malloc() and calloc() functions return a pointer to the allocated
  36. memory that is suitably aligned for any kind of variable. On error,
  37. these functions return NULL. NULL may also be returned by a successful
  38. call to malloc() with a size of zero, or by a successful call to cal‐
  39. loc() with nmemb or size equal to zero.
  40. The free() function returns no value.
  41. The realloc() function returns a pointer to the newly allocated memory,
  42. which is suitably aligned for any kind of variable and may be different
  43. from ptr, or NULL if the request fails. If size was equal to 0, either
  44. NULL or a pointer suitable to be passed to free() is returned. If
  45. realloc() fails the original block is left untouched; it is not freed
  46. or moved.
  47. CONFORMING TO
  48. C89, C99.
  49. NOTES
  50. By default, Linux follows an optimistic memory allocation strategy.
  51. This means that when malloc() returns non-NULL there is no guarantee
  52. that the memory really is available. In case it turns out that the
  53. system is out of memory, one or more processes will be killed by the
  54. OOM killer. For more information, see the description of
  55. /proc/sys/vm/overcommit_memory and /proc/sys/vm/oom_adj in proc(5), and
  56. the kernel source file Documentation/vm/overcommit-accounting.
  57. Normally, malloc() allocates memory from the heap, and adjusts the size
  58. of the heap as required, using sbrk(2). When allocating blocks of mem‐
  59. ory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation
  60. allocates the memory as a private anonymous mapping using mmap(2).
  61. MMAP_THRESHOLD is 128 kB by default, but is adjustable using mal‐
  62. lopt(3). Allocations performed using mmap(2) are unaffected by the
  63. RLIMIT_DATA resource limit (see getrlimit(2)).
  64. The UNIX 98 standard requires malloc(), calloc(), and realloc() to set
  65. errno to ENOMEM upon failure. Glibc assumes that this is done (and the
  66. glibc versions of these routines do this); if you use a private malloc
  67. implementation that does not set errno, then certain library routines
  68. may fail without having a reason in errno.
  69. Crashes in malloc(), calloc(), realloc(), or free() are almost always
  70. related to heap corruption, such as overflowing an allocated chunk or
  71. freeing the same pointer twice.
  72. Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
  73. include a malloc() implementation which is tunable via environment
  74. variables. When MALLOC_CHECK_ is set, a special (less efficient)
  75. implementation is used which is designed to be tolerant against simple
  76. errors, such as double calls of free() with the same argument, or over‐
  77. runs of a single byte (off-by-one bugs). Not all such errors can be
  78. protected against, however, and memory leaks can result. If MAL‐
  79. LOC_CHECK_ is set to 0, any detected heap corruption is silently
  80. ignored; if set to 1, a diagnostic message is printed on stderr; if set
  81. to 2, abort(3) is called immediately; if set to 3, a diagnostic message
  82. is printed on stderr and the program is aborted. Using a nonzero MAL‐
  83. LOC_CHECK_ value can be useful because otherwise a crash may happen
  84. much later, and the true cause for the problem is then very hard to
  85. track down.
  86. SEE ALSO
  87. brk(2), mallopt(3), mmap(2), alloca(3), posix_memalign(3)
  88. COLOPHON
  89. This page is part of release 3.35 of the Linux man-pages project. A
  90. description of the project, and information about reporting bugs, can
  91. be found at http://man7.org/linux/man-pages/.
  92. GNU 2011-09-08 MALLOC(3)