blob: 549880611d6098955e68cd1220064eac2f8a3e2a [file] [log] [blame]
<html lang="en">
<head>
<title>Malloc Examples - 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="Basic-Allocation.html#Basic-Allocation" title="Basic Allocation">
<link rel="next" href="Freeing-after-Malloc.html#Freeing-after-Malloc" title="Freeing after Malloc">
<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="Malloc-Examples"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Freeing-after-Malloc.html#Freeing-after-Malloc">Freeing after Malloc</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Basic-Allocation.html#Basic-Allocation">Basic Allocation</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Unconstrained-Allocation.html#Unconstrained-Allocation">Unconstrained Allocation</a>
<hr>
</div>
<h5 class="subsubsection">3.2.2.2 Examples of <code>malloc</code></h5>
<p>If no more space is available, <code>malloc</code> returns a null pointer.
You should check the value of <em>every</em> call to <code>malloc</code>. It is
useful to write a subroutine that calls <code>malloc</code> and reports an
error if the value is a null pointer, returning only if the value is
nonzero. This function is conventionally called <code>xmalloc</code>. Here
it is:
<pre class="smallexample"> void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
</pre>
<p>Here is a real example of using <code>malloc</code> (by way of <code>xmalloc</code>).
The function <code>savestring</code> will copy a sequence of characters into
a newly allocated null-terminated string:
<pre class="smallexample"> char *
savestring (const char *ptr, size_t len)
{
register char *value = (char *) xmalloc (len + 1);
value[len] = '\0';
return (char *) memcpy (value, ptr, len);
}
</pre>
<p>The block that <code>malloc</code> gives you is guaranteed to be aligned so
that it can hold any type of data. In the GNU system, the address is
always a multiple of eight on most systems, and a multiple of 16 on
64-bit systems. Only rarely is any higher boundary (such as a page
boundary) necessary; for those cases, use <code>memalign</code>,
<code>posix_memalign</code> or <code>valloc</code> (see <a href="Aligned-Memory-Blocks.html#Aligned-Memory-Blocks">Aligned Memory Blocks</a>).
<p>Note that the memory located after the end of the block is likely to be
in use for something else; perhaps a block already allocated by another
call to <code>malloc</code>. If you attempt to treat the block as longer than
you asked for it to be, you are liable to destroy the data that
<code>malloc</code> uses to keep track of its blocks, or you may destroy the
contents of another block. If you have already allocated a block and
discover you want it to be bigger, use <code>realloc</code> (see <a href="Changing-Block-Size.html#Changing-Block-Size">Changing Block Size</a>).
</body></html>