memory allocation

You're viewing an older version of this page (#3275). View the current version.

View versions (4)

NAME

<span class="Nm" id="malloc">malloc</span>, <span class="Nm" id="calloc">calloc</span>, <span class="Nm" id="valloc">valloc</span>, <span class="Nm" id="realloc">realloc</span>, <span class="Nm" id="reallocf">reallocf</span>, <span class="Nm" id="free">free</span>, <span class="Nm" id="malloc_size">malloc_size</span>, <span class="Nm" id="malloc_good_size">malloc_good_size</span>

INTERFACE

#include <stdlib.h> void malloc ( size_t size ) void calloc ( size_t count, size_t size ) void valloc ( size_t size ) void realloc ( void *ptr, size_t size ) void reallocf ( void *ptr, size_t size ) void free ( void *ptr ) size_t malloc_size ( void *ptr ) size_t malloc_good_size ( size_t size )

DESCRIPTION

The malloc , calloc , valloc , realloc , and reallocf functions allocate memory. The allocated memory is aligned such that it can be used for any data type, including AltiVec-related types. The free function frees allocations that were created via the preceding allocation functions. The malloc_size and malloc_good_size functions provide information related to the amount of padding space at the end of allocations. &#92;n &#92;n The malloc function allocates &#92;c size bytes of memory and returns a pointer to the allocated memory. malloc returns a <span class="Dv">NULL</span> pointer if there is an error. &#92;n &#92;n The calloc function contiguously allocates enough space for &#92;c count objects that are &#92;c size bytes of memory each and returns a pointer to the allocated memory. The allocated memory is filled with bytes of value zero. calloc returns a <span class="Dv">NULL</span> pointer if there is an error. &#92;n &#92;n The valloc function allocates &#92;c size bytes of memory and returns a pointer to the allocated memory. The allocated memory is aligned on a page boundary. valloc returns a <span class="Dv">NULL</span> pointer if there is an error. &#92;n &#92;n The realloc function tries to change the size of the allocation pointed to by &#92;c ptr to &#92;c size, and return &#92;c ptr. If there is not enough room to enlarge the memory allocation pointed to by &#92;c ptr, realloc creates a new allocation, copies as much of the old data pointed to by &#92;c ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory. realloc returns a <span class="Dv">NULL</span> pointer if there is an error, and the allocation pointed to by &#92;c ptr is still valid. &#92;n &#92;n The reallocf function is identical to the realloc function, except that it will free the passed pointer when the requested memory cannot be allocated. This is a FreeBSD specific API designed to ease the problems with traditional coding styles for realloc causing memory leaks in libraries. &#92;n &#92;n The free function deallocates the memory allocation pointed to by &#92;c ptr. &#92;n &#92;n The malloc_size function returns the size of the memory block that backs the allocation pointed to by &#92;c ptr. The memory block size is always at least as large as the allocation it backs, and may be larger. &#92;n &#92;n The malloc_good_size function rounds &#92;c size up to a value that the allocator implementation can allocate without adding any padding and returns that rounded up value.

RETURN VALUES

If successful, the malloc , calloc , and valloc functions return a pointer to allocated memory. If there is an error, they return a <span class="Dv">NULL</span> pointer and set <span class="Va">errno</span> to <span class="Er">ENOMEM</span>. &#92;n &#92;n If successful, the realloc and reallocf functions return a pointer to allocated memory. If there is an error, it returns a <span class="Dv">NULL</span> pointer and sets <span class="Va">errno</span> to <span class="Er">ENOMEM</span>. &#92;n &#92;n The free function does not return a value.

DEBUGGING ALLOCATION ERRORS

A number of facilities are provided to aid in debugging allocation errors in applications. These facilities are primarily controlled via environment variables. The recognized environment variables and their meanings are documented below.

ENVIRONMENT

The following environment variables change the behavior of the allocation-related functions. <table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> <span class="Ev">MallocLogFile</span> <span class="Ev"><f></span> </td> <td valign="top"> Create/append messages to the given file path &#92;c <f> instead of writing to the standard error. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocGuardEdges</span> </td> <td valign="top"> If set, add a guard page before and after each large block. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocDoNotProtectPrelude</span> </td> <td valign="top"> If set, do not add a guard page before large blocks, even if the <span class="Ev">MallocGuardEdges</span> environment variable is set. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocDoNotProtectPostlude</span> </td> <td valign="top"> If set, do not add a guard page after large blocks, even if the <span class="Ev">MallocGuardEdges</span> environment variable is set. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocStackLogging</span> </td> <td valign="top"> If set, record all stacks, so that tools like <span class="Nm">leaks</span> can be used. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocStackLoggingNoCompact</span> </td> <td valign="top"> If set, record all stacks in a manner that is compatible with the <span class="Nm">malloc_history</span> program. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocPreScribble</span> </td> <td valign="top"> If set, fill memory that has been allocated with 0xaa bytes. This increases the likelihood that a program making assumptions about the contents of freshly allocated memory will fail. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocScribble</span> </td> <td valign="top"> If set, fill memory that has been deallocated with 0x55 bytes. This increases the likelihood that a program will fail due to accessing memory that is no longer allocated. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocCheckHeapStart</span> <span class="Ev"><s></span> </td> <td valign="top"> If set, specifies the number of allocations &#92;c <s> to wait before begining periodic heap checks every &#92;c <n> as specified by <span class="Ev">MallocCheckHeapEach</span>. If <span class="Ev">MallocCheckHeapStart</span> is set but <span class="Ev">MallocCheckHeapEach</span> is not specified, the default check repetition is 1000. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocCheckHeapEach</span> <span class="Ev"><n></span> </td> <td valign="top"> If set, run a consistency check on the heap every &#92;c <n> operations. <span class="Ev">MallocCheckHeapEach</span> is only meaningful if <span class="Ev">MallocCheckHeapStart</span> is also set. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocCheckHeapSleep</span> <span class="Ev"><t></span> </td> <td valign="top"> Sets the number of seconds to sleep (waiting for a debugger to attach) when <span class="Ev">MallocCheckHeapStart</span> is set and a heap corruption is detected. The default is 100 seconds. Setting this to zero means not to sleep at all. Setting this to a negative number means to sleep (for the positive number of seconds) only the very first time a heap corruption is detected. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocCheckHeapAbort</span> <span class="Ev"><b></span> </td> <td valign="top"> When <span class="Ev">MallocCheckHeapStart</span> is set and this is set to a non-zero value, causes abort (3) to be called if a heap corruption is detected, instead of any sleeping. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocBadFreeAbort</span> <span class="Ev"><b></span> </td> <td valign="top"> If set to a non-zero value, causes abort (3) to be called if the pointer passed to reference:free (3) was previously freed, or is otherwise illegal. </td> </tr> <tr> <td valign="top" nowrap> <span class="Ev">MallocHelp</span> </td> <td valign="top"> If set, print a list of environment variables that are paid heed to by the allocation-related functions, along with short descriptions. The list should correspond to this documentation. </td> </tr> </table>

SEE ALSO

reference:leaks (1) , reference:malloc_history (1) , abort (3) <span class="Pa">/Developer/Documentation/ReleaseNotes/DeveloperTools/MallocOptions.html</span>