| <html lang="en"> |
| <head> |
| <title>Freeing after Malloc - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Unconstrained-Allocation.html#Unconstrained-Allocation" title="Unconstrained Allocation"> |
| <link rel="prev" href="Malloc-Examples.html#Malloc-Examples" title="Malloc Examples"> |
| <link rel="next" href="Changing-Block-Size.html#Changing-Block-Size" title="Changing Block Size"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Freeing-after-Malloc"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Changing-Block-Size.html#Changing-Block-Size">Changing Block Size</a>, |
| Previous: <a rel="previous" accesskey="p" href="Malloc-Examples.html#Malloc-Examples">Malloc Examples</a>, |
| Up: <a rel="up" accesskey="u" href="Unconstrained-Allocation.html#Unconstrained-Allocation">Unconstrained Allocation</a> |
| <hr> |
| </div> |
| |
| <h5 class="subsubsection">3.2.2.3 Freeing Memory Allocated with <code>malloc</code></h5> |
| |
| <p><a name="index-freeing-memory-allocated-with-_0040code_007bmalloc_007d-260"></a><a name="index-heap_002c-freeing-memory-from-261"></a> |
| When you no longer need a block that you got with <code>malloc</code>, use the |
| function <code>free</code> to make the block available to be allocated again. |
| The prototype for this function is in <samp><span class="file">stdlib.h</span></samp>. |
| <a name="index-stdlib_002eh-262"></a> |
| <!-- malloc.h stdlib.h --> |
| <!-- ISO --> |
| |
| <div class="defun"> |
| — Function: void <b>free</b> (<var>void *ptr</var>)<var><a name="index-free-263"></a></var><br> |
| <blockquote><p>The <code>free</code> function deallocates the block of memory pointed at |
| by <var>ptr</var>. |
| </p></blockquote></div> |
| |
| <!-- stdlib.h --> |
| <!-- Sun --> |
| <div class="defun"> |
| — Function: void <b>cfree</b> (<var>void *ptr</var>)<var><a name="index-cfree-264"></a></var><br> |
| <blockquote><p>This function does the same thing as <code>free</code>. It's provided for |
| backward compatibility with SunOS; you should use <code>free</code> instead. |
| </p></blockquote></div> |
| |
| <p>Freeing a block alters the contents of the block. <strong>Do not expect to |
| find any data (such as a pointer to the next block in a chain of blocks) in |
| the block after freeing it.</strong> Copy whatever you need out of the block before |
| freeing it! Here is an example of the proper way to free all the blocks in |
| a chain, and the strings that they point to: |
| |
| <pre class="smallexample"> struct chain |
| { |
| struct chain *next; |
| char *name; |
| } |
| |
| void |
| free_chain (struct chain *chain) |
| { |
| while (chain != 0) |
| { |
| struct chain *next = chain->next; |
| free (chain->name); |
| free (chain); |
| chain = next; |
| } |
| } |
| </pre> |
| <p>Occasionally, <code>free</code> can actually return memory to the operating |
| system and make the process smaller. Usually, all it can do is allow a |
| later call to <code>malloc</code> to reuse the space. In the meantime, the |
| space remains in your program as part of a free-list used internally by |
| <code>malloc</code>. |
| |
| <p>There is no point in freeing blocks at the end of a program, because all |
| of the program's space is given back to the system when the process |
| terminates. |
| |
| </body></html> |
| |