'\" te .\" Copyright 1989 AT&T. Copyright (c) 2005, Sun Microsystems, Inc. All Rights Reserved. .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] .TH malloc 3C "21 Mar 2005" "SunOS 5.11" "Standard C Library Functions" .SH NAME malloc, calloc, free, memalign, realloc, valloc, alloca \- memory allocator .SH SYNOPSIS .LP .nf #include \fBvoid *\fR\fBmalloc\fR(\fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid *\fR\fBcalloc\fR(\fBsize_t\fR \fInelem\fR, \fBsize_t\fR \fIelsize\fR); .fi .LP .nf \fBvoid\fR \fBfree\fR(\fBvoid *\fR\fIptr\fR); .fi .LP .nf \fBvoid *\fR\fBmemalign\fR(\fBsize_t\fR \fIalignment\fR, \fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid *\fR\fBrealloc\fR(\fBvoid *\fR\fIptr\fR, \fBsize_t\fR \fIsize\fR); .fi .LP .nf \fBvoid *\fR\fBvalloc\fR(\fBsize_t\fR \fIsize\fR); .fi .LP .nf #include \fBvoid *\fR\fBalloca\fR(\fBsize_t\fR \fIsize\fR); .fi .SH DESCRIPTION .sp .LP The \fBmalloc()\fR and \fBfree()\fR functions provide a simple, general-purpose memory allocation package. The \fBmalloc()\fR function returns a pointer to a block of at least \fIsize\fR bytes suitably aligned for any use. If the space assigned by \fBmalloc()\fR is overrun, the results are undefined. .sp .LP The argument to \fBfree()\fR is a pointer to a block previously allocated by \fBmalloc()\fR, \fBcalloc()\fR, or \fBrealloc()\fR. After \fBfree()\fR is executed, this space is made available for further allocation by the application, though not returned to the system. Memory is returned to the system only upon termination of the application. If \fIptr\fR is a null pointer, no action occurs. If a random number is passed to \fBfree()\fR, the results are undefined. .sp .LP The \fBcalloc()\fR function allocates space for an array of \fInelem\fR elements of size \fIelsize\fR. The space is initialized to zeros. .sp .LP The \fBmemalign()\fR function allocates \fIsize\fR bytes on a specified alignment boundary and returns a pointer to the allocated block. The value of the returned address is guaranteed to be an even multiple of \fIalignment\fR. The value of \fIalignment\fR must be a power of two and must be greater than or equal to the size of a word. .sp .LP The \fBrealloc()\fR function changes the size of the block pointed to by \fIptr\fR to \fIsize\fR 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. If the new size of the block requires movement of the block, the space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the block are unspecified. If \fIptr\fR is \fINULL\fR, \fBrealloc()\fR behaves like \fBmalloc()\fR for the specified size. If \fIsize\fR is 0 and \fIptr\fR is not a null pointer, the space pointed to is freed. .sp .LP The \fBvalloc()\fR function has the same effect as \fBmalloc()\fR, except that the allocated memory will be aligned to a multiple of the value returned by \fBsysconf\fR(\fB_SC_PAGESIZE\fR). .sp .LP The \fBalloca()\fR function allocates \fIsize\fR bytes of space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the caller returns. If the allocated block is beyond the current stack limit, the resulting behavior is undefined. .SH RETURN VALUES .sp .LP Upon successful completion, each of the allocation functions returns a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object. .sp .LP If there is no available memory, \fBmalloc()\fR, \fBrealloc()\fR, \fBmemalign()\fR, \fBvalloc()\fR, and \fBcalloc()\fR return a null pointer. When \fBrealloc()\fR is called with \fIsize\fR > 0 and returns \fINULL\fR, the block pointed to by \fIptr\fR is left intact. If \fIsize\fR, \fInelem\fR, or \fIelsize\fR is 0, either a null pointer or a unique pointer that can be passed to \fBfree()\fR is returned. .sp .LP If \fBmalloc()\fR, \fBcalloc()\fR, or \fBrealloc()\fR returns unsuccessfully, \fBerrno\fR will be set to indicate the error. The \fBfree()\fR function does not set \fBerrno\fR. .SH ERRORS .sp .LP The \fBmalloc()\fR, \fBcalloc()\fR, and \fBrealloc()\fR functions will fail if: .sp .ne 2 .mk .na \fB\fBENOMEM\fR\fR .ad .RS 10n .rt The physical limits of the system are exceeded by \fIsize\fR bytes of memory which cannot be allocated. .RE .sp .ne 2 .mk .na \fB\fBEAGAIN\fR\fR .ad .RS 10n .rt There is not enough memory available to allocate \fIsize\fR bytes of memory; but the application could try again later. .RE .SH USAGE .sp .LP Portable applications should avoid using \fBvalloc()\fR but should instead use \fBmalloc()\fR or \fBmmap\fR(2). On systems with a large page size, the number of successful \fBvalloc()\fR operations might be 0. .sp .LP These default memory allocation routines are safe for use in multithreaded applications but are not scalable. Concurrent accesses by multiple threads are single-threaded through the use of a single lock. Multithreaded applications that make heavy use of dynamic memory allocation should be linked with allocation libraries designed for concurrent access, such as \fBlibumem\fR(3LIB) or \fBlibmtmalloc\fR(3LIB). Applications that want to avoid using heap allocations (with \fBbrk\fR(2)) can do so by using either \fBlibumem\fR or \fBlibmapmalloc\fR(3LIB). The allocation libraries \fBlibmalloc\fR(3LIB) and \fBlibbsdmalloc\fR(3LIB) are available for special needs. .sp .LP Comparative features of the various allocation libraries can be found in the \fBumem_alloc\fR(3MALLOC) manual page. .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ Interface StabilitySee below. _ MT-LevelSafe .TE .sp .LP The \fBmalloc()\fR, \fBcalloc()\fR, \fBfree()\fR, \fBrealloc()\fR, \fBvalloc()\fR functions are Standard. The \fBmemalign()\fR and \fBalloca()\fR functions are Stable. .SH SEE ALSO .sp .LP \fBbrk\fR(2), \fBgetrlimit\fR(2), \fBlibbsdmalloc\fR(3LIB), \fBlibmalloc\fR(3LIB), \fBlibmapmalloc\fR(3LIB), \fBlibmtmalloc\fR(3LIB), \fBlibumem\fR(3LIB), \fBumem_alloc\fR(3MALLOC), \fBwatchmalloc\fR(3MALLOC), \fBattributes\fR(5) .SH WARNINGS .sp .LP Undefined results will occur if the size requested for a block of memory exceeds the maximum size of a process's heap, which can be obtained with \fBgetrlimit\fR(2) .sp .LP The \fBalloca()\fR function is machine-, compiler-, and most of all, system-dependent. Its use is strongly discouraged.