126 lines
4.0 KiB
HTML
126 lines
4.0 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<TITLE>malloc(3)</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<H1>malloc(3)</H1>
|
|
<HR>
|
|
<PRE>
|
|
|
|
</PRE>
|
|
<H2>NAME</H2><PRE>
|
|
malloc, free, realloc, calloc, alloca - memory allocator
|
|
|
|
|
|
</PRE>
|
|
<H2>SYNOPSIS</H2><PRE>
|
|
<STRONG>#include</STRONG> <STRONG><sys/types.h></STRONG>
|
|
<STRONG>#include</STRONG> <STRONG><stdlib.h></STRONG>
|
|
<STRONG>#include</STRONG> <STRONG><alloca.h></STRONG>
|
|
|
|
<STRONG>void</STRONG> <STRONG>*malloc(size_t</STRONG> <EM>size</EM><STRONG>)</STRONG>
|
|
<STRONG>void</STRONG> <STRONG>free(void</STRONG> <STRONG>*</STRONG><EM>ptr</EM><STRONG>)</STRONG>
|
|
<STRONG>void</STRONG> <STRONG>*realloc(void</STRONG> <STRONG>*</STRONG><EM>ptr</EM><STRONG>,</STRONG> <STRONG>size_t</STRONG> <EM>size</EM><STRONG>)</STRONG>
|
|
<STRONG>void</STRONG> <STRONG>*calloc(size_t</STRONG> <EM>nelem</EM><STRONG>,</STRONG> <STRONG>size_t</STRONG> <EM>elsize</EM><STRONG>)</STRONG>
|
|
<STRONG>void</STRONG> <STRONG>*alloca(size_t</STRONG> <EM>size</EM><STRONG>)</STRONG>
|
|
|
|
|
|
</PRE>
|
|
<H2>DESCRIPTION</H2><PRE>
|
|
<STRONG>Malloc</STRONG> and <STRONG>free</STRONG> provide a general-purpose memory allocation package.
|
|
<STRONG>Malloc</STRONG> returns a pointer to a block of at least <EM>size</EM> bytes beginning on a
|
|
word boundary.
|
|
|
|
The argument to <STRONG>free</STRONG> is a pointer to a block previously allocated by
|
|
<STRONG>malloc</STRONG>; this space is made available for further allocation, but its
|
|
contents are left undisturbed. A call with a null <EM>ptr</EM> is legal and does
|
|
nothing.
|
|
|
|
Needless to say, grave disorder will result if the space assigned by
|
|
<STRONG>malloc</STRONG> is overrun or if some random number is handed to <STRONG>free</STRONG>.
|
|
|
|
<STRONG>Malloc</STRONG> maintains multiple lists of free blocks according to size,
|
|
allocating space from the appropriate list. It calls <STRONG>sbrk</STRONG> (see <STRONG><A HREF="../man2/brk.2.html">brk(2)</A></STRONG>)
|
|
to get more memory from the system when there is no suitable space
|
|
already free.
|
|
|
|
<STRONG>Realloc</STRONG> changes the size of the block pointed to by <EM>ptr</EM> to <EM>size</EM> bytes and
|
|
returns a pointer to the (possibly moved) block. The contents will be
|
|
unchanged up to the lesser of the new and old sizes. A call with a null
|
|
<EM>ptr</EM> is legal and has the same result as <STRONG>malloc(</STRONG><EM>size</EM><STRONG>)</STRONG>.
|
|
|
|
<STRONG>Calloc</STRONG> allocates space for an array of <EM>nelem</EM> elements of size <EM>elsize</EM>. The
|
|
space is initialized to zeros.
|
|
|
|
<STRONG>Alloca</STRONG> allocates <EM>size</EM> bytes of space in the stack frame of the caller.
|
|
This temporary space is automatically freed on return.
|
|
|
|
Each of the allocation routines returns a pointer to space suitably
|
|
aligned (after possible pointer coercion) for storage of any type of
|
|
object.
|
|
|
|
|
|
|
|
|
|
|
|
</PRE>
|
|
<H2>SEE ALSO</H2><PRE>
|
|
<STRONG><A HREF="../man2/brk.2.html">brk(2)</A></STRONG>.
|
|
|
|
|
|
</PRE>
|
|
<H2>DIAGNOSTICS</H2><PRE>
|
|
<STRONG>Malloc</STRONG>, <STRONG>realloc</STRONG> and <STRONG>calloc</STRONG> return a null pointer if there is no available
|
|
memory or if the arena has been detectably corrupted by storing outside
|
|
the bounds of a block.
|
|
|
|
|
|
</PRE>
|
|
<H2>NOTES</H2><PRE>
|
|
Other implementations of <STRONG>malloc</STRONG>, <STRONG>realloc</STRONG> or <STRONG>calloc</STRONG> may return a null
|
|
pointer if the size of the requested block is zero. This implementation
|
|
will always return a zero length block at a unique address, but you
|
|
should keep in mind that a null return is possible if the program is run
|
|
to another system and that this should not be mistakenly seen as an
|
|
error.
|
|
|
|
|
|
</PRE>
|
|
<H2>BUGS</H2><PRE>
|
|
When <STRONG>realloc</STRONG> returns a null pointer, the block pointed to by <EM>ptr</EM> may be
|
|
destroyed.
|
|
|
|
<STRONG>Alloca</STRONG> is machine dependent; its use is discouraged.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</PRE>
|
|
</BODY>
|
|
</HTML>
|