Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

brk(2)

malloc(3C)

malloc(3X)                                                       malloc(3X)

NAME
     malloc, free, realloc, calloc, mallopt, mallinfo - allocate memory

SYNOPSIS
     cc [flag ...] file ... -lmalloc

     #include <stdlib.h>
     #include <malloc.h>

     void *malloc(sizet size);

     void free(void *ptr);

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

     void *calloc(sizet nelem, sizet elsize);

     #include <malloc.h>

     int mallopt(int cmd, int value);

     struct mallinfo mallinfo(void);

DESCRIPTION
     malloc() and free() provide a simple general-purpose memory allocation
     package.

     malloc() returns a pointer to a block of at least size bytes. The
     space is not initialized.

     The argument to free() is a pointer to a block previously allocated by
     malloc(); after free() is performed this space is made available for
     further allocation, and its contents have been destroyed (but see
     mallopt() below for a way to change this behavior). If ptr is a null
     pointer, no action occurs.

     Undefined results occur if the space assigned by malloc() is overrun
     or if some random number is handed to 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 con-
     tents are unchanged up to the lesser of the new and old sizes.

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

     mallopt() provides for control over the allocation algorithm. The
     available values for cmd are defined in the malloc.h header file, and
     have the following meanings:






Page 1                       Reliant UNIX 5.44                Printed 11/98

malloc(3X)                                                       malloc(3X)

     MMXFAST  Set maxfast to value. The algorithm allocates all blocks
               below the size of maxfast in large groups and then doles
               them out very quickly. The default value for maxfast is 24.

     MNLBLKS  Set numlblks to value. The above-mentioned "large groups"
               each contain numlblks blocks. numlblks must be greater than
               0. The default value for numlblks is 100.

     MGRAIN   Set grain to value. The sizes of all blocks smaller than
               maxfast are considered to be rounded up to the nearest mul-
               tiple of grain. grain must be greater than 0. The default
               value of grain is the smallest number of bytes that will
               allow alignment of any data type. Value will be rounded up
               to a multiple of the default when grain is set.

     MKEEP    Preserve data in a freed block until the next malloc(),
               realloc(), or calloc().

               This option is provided only for compatibility with the old
               version of malloc() and is not recommended.

     mallopt() may be called repeatedly, but may not be called after the
     first small block is allocated.

     mallinfo() provides instrumentation describing space usage. It returns
     the structure:

        struct mallinfo  {
             int arena;      /* total space in arena */
             int ordblks;    /* number of ordinary blocks */
             int smblks;     /* number of small blocks */
             int hblkhd;     /* space in holding block headers */
             int hblks;      /* number of holding blocks */
             int usmblks;    /* space in small blocks in use */
             int fsmblks;    /* space in free small blocks */
             int uordblks;   /* space in ordinary blocks in use */
             int fordblks;   /* space in free ordinary blocks */
             int keepcost;   /* space penalty if keep option is used */
     }

     This structure is defined in the malloc.h header file.

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

RESULT
     malloc(), realloc(), and calloc() return a null pointer if there is
     not enough available memory. When realloc() returns NULL, the block
     pointed to by ptr is left intact. If mallopt() is called after any
     allocation or if cmd or value are invalid, non-zero is returned. Oth-
     erwise, it returns zero.


Page 2                       Reliant UNIX 5.44                Printed 11/98

malloc(3X)                                                       malloc(3X)

NOTES
     Note that unlike malloc(3C), this package does not preserve the con-
     tents of a block when it is freed, unless the MKEEP option of
     mallopt() is used.

     Undocumented features of malloc(3C) have not been duplicated.

     Function prototypes for malloc(), realloc(), calloc() and free() are
     also defined in the malloc.h header file for compatibility with old
     applications. New applications should include stdlib.h to access the
     prototypes for these functions.

     The algorithm used by malloc(3X) is a "first-fit" algorithm. If you
     want to use the "best-fit" algorithm, you should use malloc(3C).

SEE ALSO
     brk(2), malloc(3C).





































Page 3                       Reliant UNIX 5.44                Printed 11/98

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