man(1) Manual page archive


     MALLOC(3)                                               MALLOC(3)

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

     SYNOPSIS
          char *malloc(size)
          unsigned size;

          free(ptr)
          char *ptr;

          char *realloc(ptr, size)
          char *ptr;
          unsigned size;

          char *calloc(nelem, elsize)
          unsigned nelem, elsize;

          cfree(ptr)
          char *ptr;

     DESCRIPTION
          Malloc and free provide a simple memory allocation package.
          Malloc returns a pointer to a new block of at least size
          bytes.  The block is suitably aligned for storage of any
          type of object.  No two active pointers from malloc will
          have the same value.

          The argument to free is a pointer to a block previously
          allocated by malloc; this space is made available for fur-
          ther allocation.

          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.  The call realloc((char*)0, size)
          means the same as `malloc(size)'.

          Calloc allocates space for an array of nelem elements of
          size elsize. The space is initialized to zeros.  Cfree frees
          such a block.

     SEE ALSO
          galloc(3), brk(2), pool(3), block(3)

     DIAGNOSTICS
          Malloc, realloc and calloc return 0 if there is no available
          memory or if the arena has been detectably corrupted.

     BUGS
          When realloc returns 0, the block pointed to by ptr may have

     MALLOC(3)                                               MALLOC(3)

          been destroyed.

          User errors can corrupt the storage arena.  The most common
          gaffes are (1) freeing an already freed block, (2) storing
          beyond the bounds of an allocated block, and (3) freeing
          data that was not obtained from the allocator.  To help find
          such errors, a diagnosing allocator may be loaded; use flag
          -ldmalloc of cc(1). An even more stringently checking ver-
          sion may be created by recompilation; see the source.