Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

brk(2)

cc(1)

malloc(3X)

malloc.dbg(3C)

MALLOC(3C)                           SysV                           MALLOC(3C)



NAME
     malloc, free, realloc, calloc - main memory allocator

SYNOPSIS
     #include <stdlib.h>

     void *malloc (size)
     size_t size;

     void free (ptr)
     void *ptr;

     void *realloc (ptr, size)
     void *ptr;
     size_t size;

     void *calloc (nelem, elsize)
     size_t nelem, elsize;

DESCRIPTION
     malloc and free comprise a simple general-purpose memory allocation
     package.  malloc returns a pointer to a block of at least size bytes
     suitably aligned for any use.

     The argument to free is a pointer to a block previously allocated by
     malloc; free makes this space available for further allocation, but
     leaves its contents undisturbed.

     Undefined results will occur if the space assigned by malloc is overrun
     or if some random number is handed to free.

     malloc allocates the first contiguous reach of size byes of free space
     that it finds in a circular search from the last block allocated or
     freed.  It coalesces adjacent free blocks as it searches.  It calls sbrk
     (see brk(2)) to get more memory from the system when there is
     insufficient space already free.

     realloc changes the size of the block pointed to by ptr to size 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 no free
     block of size bytes is available in the storage arena, then realloc will
     ask malloc to enlarge the arena by size bytes and will then move the data
     to the new space.

     realloc also works if ptr points to a block freed since the last call of
     malloc, realloc, or calloc.  Thus, sequences of free, malloc and realloc
     can exploit the search strategy of malloc to do storage compaction.

     calloc allocates space for an array of nelem elements of size elsize.
     The space is initialized to zeros.

     Each of the allocation routines returns a pointer to space suitably
     aligned (after possible pointer coercion) for storage of any type of
     object.

NOTES
     Developers of installed libraries should include <apollo/shlib.h> in
     those modules which use malloc, free, or realloc.  Doing so assures that
     references to these routines resolve to those defined by the application,
     if any.

     To be compatible with common C usage, make malloc, calloc, and realloc
     return type pointer-to-character by compiling your program with the

          -A nansi

     option (see cc(1)) and inserting the directive

          #define _CLASSIC_TYPES

     before any #include directives.

SEE ALSO
     brk(2), cc(1), malloc(3X), malloc.dbg(3C).

DIAGNOSTICS
     malloc, realloc and calloc 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.  When this happens, the block pointed to by ptr
     may be destroyed.

NOTES
     Search time increases when many objects have been allocated; that is, if
     a program allocates but never frees, then each successive allocation
     takes longer.  For an alternate, more flexible implementation, see
     malloc(3X).

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026