os161 / man / libc / malloc.html
malloc.html
Raw
<!--
Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2013
	The President and Fellows of Harvard College.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
-->
<html>
<head>
<title>malloc</title>
<body bgcolor=#ffffff>
<h2 align=center>malloc</h2>
<h4 align=center>OS/161 Reference Manual</h4>

<h3>Name</h3>
<p>
malloc - allocate memory
</p>

<h3>Library</h3>
<p>
Standard C Library (libc, -lc)
</p>

<h3>Synopsis</h3>
<p>
<tt>#include &lt;stdlib.h&gt;</tt><br>
<br>
<tt>void *</tt><br>
<tt>malloc(size_t </tt><em>size</em><tt>);</tt>
</p>

<h3>Description</h3>
<p>
<tt>malloc</tt> allocates <em>size</em> bytes of memory and returns a
pointer to it. The memory is not necessarily zero-filled. (To get
zero-filled memory, call <A HREF=bzero.html>bzero</A> or
<A HREF=memset.html>memset</A>, or use
<A HREF=calloc.html>calloc</A>.)
</p>

<p>
The pointer returned must be suitably aligned for use with any data
type.
</p>

<p>
When asked to allocate zero bytes, <tt>malloc</tt> may either always
return <tt>NULL</tt>, or may return distinct non-null pointers that do
not point to any storage.
</p>

<p>
While <tt>malloc</tt> may at its option allocate more than
<em>size</em> bytes to fill a request, code that calls <tt>malloc</tt>
may not depend on such behavior and must not perform any accesses
outside of the bounds defined by <em>size</em>.
</p>

<p>
It is legitimate for memory returned by malloc to not actually be
physically mapped until it is used.
If at the time it is used, no physical memory is available and there
is no space to swap something out to make room, the process may
potentially receive a fatal signal or be killed.
This behavior is often somewhat contentious; a full discussion of the
possible alternatives and their pros and cons is well beyond the scope
of this man page.
</p>

<h3>Return Values</h3>
<p>
<tt>malloc</tt> returns a pointer to the memory allocated. If memory
cannot be obtained, NULL is returned.
</p>

<h3>See Also</h3>
<p>
<A HREF=calloc.html>calloc</A>,
<A HREF=realloc.html>realloc</A>,
<A HREF=free.html>free</A>
</p>

</body>
</html>