Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ malloc(3) — AIX/RT 2.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

malloc, free, realloc, calloc

Purpose

     Provides a means to allocate memory.

Library

     Standard C Library (libc.a)

Syntax

     char *malloc (size)                       char *realloc (ptr, size)
     unsigned int size;                        char *ptr;
                                               unsigned int size;
     void free (ptr)
     char *ptr;                                char *calloc (nelem, elsize)
                                               unsigned int nelem, elsize;
     Description

     The malloc and free  subroutines provide a simple general
     purpose memory allocation package.

     The malloc  subroutine returns  a pointer  to a  block of
     memory of at  least size bytes.  The block  is aligned so
     that  it can  be used  for any  type of  data.  Undefined
     results occur if the space assigned by malloc is overrun.

     The malloc subroutine searches  memory for the first con-
     tiguous area of  free space of at least  size bytes.  The
     search is performed  in a circular pattern  from the last
     block of  memory allocated or freed.   During the search,
     malloc joins adjacent free blocks  of memory.  If a large
     enough contiguous area  of free space is  not found, then
     malloc issues a sbrk system  call to get more memory from
     the system.

     The free subroutine frees the  block of memory pointed to
     by the  ptr parameter for further  allocation.  The block
     pointed to by the ptr parameter must have been previously
     allocated by the malloc  subroutine.  The free subroutine
     does not  change the  contents of  this block  of memory.
     Undefined results  occur if  the ptr  parameter is  not a
     valid pointer.

     The realloc subroutine  changes the size of  the block of
     memory pointed to  by the ptr parameter to  the number of
     bytes  specified  by  the  size parameter,  and  then  it
     returns  a pointer  to the  block.  The  contents of  the
     block remain  unchanged up to  the lesser of the  old and
     new  sizes.  If  a large  enough block  of memory  is not
     available, then  realloc calls  the malloc  subroutine to
     enlarge the memory arena, and  then moves the data to the
     new space.

     The realloc  subroutine also  works if the  ptr parameter
     points to  a block freed  since the last call  to malloc,
     realloc, or calloc.

     The calloc  subroutine allocates space for  an array with
     the number of elements  specified by the nelem parameter.
     Each  element is  of  the size  specified  by the  elsize
     parameter.  The space is initialized to 0's.

     Each of  the allocation subroutines returns  a pointer to
     space suitably aligned for storage of any type of object.
     Cast the  pointer to  the type  pointer-to-element before
     using it.

     The malloc, realloc, and calloc subroutines return a NULL
     pointer if there is no  available memory or if the memory
     arena has been corrupted by storing outside the bounds of
     a block.  When this happens,  the block pointed to by the
     ptr parameter could be destroyed.

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