MALLOC(3)

NAME
malloc, free, realloc, calloc, valloc, malloc_size, malloc_good_size, alloca - memory allocator

SYNOPSIS
#include <stdlib.h>

void *malloc(size_t byteSize)

void free(void *ptr)

void *realloc(void *oldptr, size_t newsize)

void *calloc(size_t numElems, size_t byteSize)

void *valloc(size_t byteSize)

size_t malloc_size(void *ptr)

size_t malloc_good_size(size_t byteSize)

char *alloca(int size)

DESCRIPTION
Malloc and free provide a general-purpose memory allocation package. Malloc returns a pointer to a block of at least size bytes beginning on a long boundary.

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

Needless to say, grave disorder will result 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 contents will be unchanged up to the lesser of the new and old sizes.

Calloc allocates space for an array of nelem elements of size elsize. The space is initialized to zeros. Pointers allocated by calloc should be freed using free.

Valloc allocates a page-aligned space of size at least byteSize. Pointers allocated by valloc should be freed using free.

Malloc_size returns the size of the block pointed to by the argument. The size is always at least the requested size, and is often a few words larger.

Malloc_good_size returns a size that is unlikely to waste any storage.

Alloca allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed on return.

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

DIAGNOSTICS
Malloc, realloc and calloc return a null pointer (NULL) if there is no available memory.

DEBUGGING ALLOCATION ERRORS
This package contains a number of facilities to help debug. (These facilities are subject to change; therefore, shipping products should not rely on the presence of any of these facilities at runtime.) To list these facilities set the environment variable "MallocHelp" and then launch the program from the environment in which the variable is set. For example, in csh you would execute "setenv MallocHelp 1" and then launch your program from the command line. A list of environment variables and their effects will be printed to the shell.

BUGS
Malloc should guarantee nothing about the state of a block once it has been passed to free. However, some other versions of malloc will guarantee that a block that has been freed is left unchanged until a subsequent call to free, realloc, or malloc.

Alloca is machine dependent; its use is discouraged.