You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>malloc (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="malloc (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="malloc (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Stdlib.html#Stdlib" rel="up" title="Stdlib">
  16. <link href="mallinfo.html#mallinfo" rel="next" title="mallinfo">
  17. <link href="lldiv.html#lldiv" rel="prev" title="lldiv">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="malloc"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="mallinfo.html#mallinfo" accesskey="n" rel="next">mallinfo</a>, Previous: <a href="lldiv.html#lldiv" accesskey="p" rel="prev">lldiv</a>, Up: <a href="Stdlib.html#Stdlib" accesskey="u" rel="up">Stdlib</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="malloc_002c-realloc_002c-free_002d_002d_002dmanage-memory"></a>
  54. <h3 class="section">2.24 <code>malloc</code>, <code>realloc</code>, <code>free</code>&mdash;manage memory</h3>
  55. <a name="index-malloc"></a>
  56. <a name="index-realloc"></a>
  57. <a name="index-reallocf"></a>
  58. <a name="index-free"></a>
  59. <a name="index-memalign"></a>
  60. <a name="index-malloc_005fusable_005fsize"></a>
  61. <a name="index-_005fmalloc_005fr"></a>
  62. <a name="index-_005frealloc_005fr"></a>
  63. <a name="index-_005freallocf_005fr"></a>
  64. <a name="index-_005ffree_005fr"></a>
  65. <a name="index-_005fmemalign_005fr"></a>
  66. <a name="index-_005fmalloc_005fusable_005fsize_005fr"></a>
  67. <p><strong>Synopsis</strong>
  68. </p><div class="example">
  69. <pre class="example">#include &lt;stdlib.h&gt;
  70. void *malloc(size_t <var>nbytes</var>);
  71. void *realloc(void *<var>aptr</var>, size_t <var>nbytes</var>);
  72. void *reallocf(void *<var>aptr</var>, size_t <var>nbytes</var>);
  73. void free(void *<var>aptr</var>);
  74. void *memalign(size_t <var>align</var>, size_t <var>nbytes</var>);
  75. size_t malloc_usable_size(void *<var>aptr</var>);
  76. void *_malloc_r(void *<var>reent</var>, size_t <var>nbytes</var>);
  77. void *_realloc_r(void *<var>reent</var>,
  78. void *<var>aptr</var>, size_t <var>nbytes</var>);
  79. void *_reallocf_r(void *<var>reent</var>,
  80. void *<var>aptr</var>, size_t <var>nbytes</var>);
  81. void _free_r(void *<var>reent</var>, void *<var>aptr</var>);
  82. void *_memalign_r(void *<var>reent</var>,
  83. size_t <var>align</var>, size_t <var>nbytes</var>);
  84. size_t _malloc_usable_size_r(void *<var>reent</var>, void *<var>aptr</var>);
  85. </pre></div>
  86. <p><strong>Description</strong><br>
  87. These functions manage a pool of system memory.
  88. </p>
  89. <p>Use <code>malloc</code> to request allocation of an object with at least
  90. <var>nbytes</var> bytes of storage available. If the space is available,
  91. <code>malloc</code> returns a pointer to a newly allocated block as its result.
  92. </p>
  93. <p>If you already have a block of storage allocated by <code>malloc</code>, but
  94. you no longer need all the space allocated to it, you can make it
  95. smaller by calling <code>realloc</code> with both the object pointer and the
  96. new desired size as arguments. <code>realloc</code> guarantees that the
  97. contents of the smaller object match the beginning of the original object.
  98. </p>
  99. <p>Similarly, if you need more space for an object, use <code>realloc</code> to
  100. request the larger size; again, <code>realloc</code> guarantees that the
  101. beginning of the new, larger object matches the contents of the
  102. original object.
  103. </p>
  104. <p>When you no longer need an object originally allocated by <code>malloc</code>
  105. or <code>realloc</code> (or the related function <code>calloc</code>), return it to the
  106. memory storage pool by calling <code>free</code> with the address of the object
  107. as the argument. You can also use <code>realloc</code> for this purpose by
  108. calling it with <code>0</code> as the <var>nbytes</var> argument.
  109. </p>
  110. <p>The <code>reallocf</code> function behaves just like <code>realloc</code> except if the
  111. function is required to allocate new storage and this fails. In this
  112. case <code>reallocf</code> will free the original object passed in whereas
  113. <code>realloc</code> will not.
  114. </p>
  115. <p>The <code>memalign</code> function returns a block of size <var>nbytes</var> aligned
  116. to a <var>align</var> boundary. The <var>align</var> argument must be a power of
  117. two.
  118. </p>
  119. <p>The <code>malloc_usable_size</code> function takes a pointer to a block
  120. allocated by <code>malloc</code>. It returns the amount of space that is
  121. available in the block. This may or may not be more than the size
  122. requested from <code>malloc</code>, due to alignment or minimum size
  123. constraints.
  124. </p>
  125. <p>The alternate functions <code>_malloc_r</code>, <code>_realloc_r</code>, <code>_reallocf_r</code>,
  126. <code>_free_r</code>, <code>_memalign_r</code>, and <code>_malloc_usable_size_r</code> are reentrant
  127. versions. The extra argument <var>reent</var> is a pointer to a reentrancy structure.
  128. </p>
  129. <p>If you have multiple threads of execution which may call any of these
  130. routines, or if any of these routines may be called reentrantly, then
  131. you must provide implementations of the <code>__malloc_lock</code> and
  132. <code>__malloc_unlock</code> functions for your system. See the documentation
  133. for those functions.
  134. </p>
  135. <p>These functions operate by calling the function <code>_sbrk_r</code> or
  136. <code>sbrk</code>, which allocates space. You may need to provide one of these
  137. functions for your system. <code>_sbrk_r</code> is called with a positive
  138. value to allocate more space, and with a negative value to release
  139. previously allocated space if it is no longer required.
  140. See <a href="Stubs.html#Stubs">Stubs</a>.
  141. </p>
  142. <br>
  143. <p><strong>Returns</strong><br>
  144. <code>malloc</code> returns a pointer to the newly allocated space, if
  145. successful; otherwise it returns <code>NULL</code>. If your application needs
  146. to generate empty objects, you may use <code>malloc(0)</code> for this purpose.
  147. </p>
  148. <p><code>realloc</code> returns a pointer to the new block of memory, or <code>NULL</code>
  149. if a new block could not be allocated. <code>NULL</code> is also the result
  150. when you use &lsquo;<code>realloc(<var>aptr</var>,0)</code>&rsquo; (which has the same effect as
  151. &lsquo;<code>free(<var>aptr</var>)</code>&rsquo;). You should always check the result of
  152. <code>realloc</code>; successful reallocation is not guaranteed even when
  153. you request a smaller object.
  154. </p>
  155. <p><code>free</code> does not return a result.
  156. </p>
  157. <p><code>memalign</code> returns a pointer to the newly allocated space.
  158. </p>
  159. <p><code>malloc_usable_size</code> returns the usable size.
  160. </p>
  161. <br>
  162. <p><strong>Portability</strong><br>
  163. <code>malloc</code>, <code>realloc</code>, and <code>free</code> are specified by the ANSI C
  164. standard, but other conforming implementations of <code>malloc</code> may
  165. behave differently when <var>nbytes</var> is zero.
  166. </p>
  167. <p><code>memalign</code> is part of SVR4.
  168. </p>
  169. <p><code>malloc_usable_size</code> is not portable.
  170. </p>
  171. <p>Supporting OS subroutines required: <code>sbrk</code>.
  172. </p><br>
  173. <hr>
  174. <div class="header">
  175. <p>
  176. Next: <a href="mallinfo.html#mallinfo" accesskey="n" rel="next">mallinfo</a>, Previous: <a href="lldiv.html#lldiv" accesskey="p" rel="prev">lldiv</a>, Up: <a href="Stdlib.html#Stdlib" accesskey="u" rel="up">Stdlib</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  177. </div>
  178. </body>
  179. </html>