Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ calloc(3C) — HP-UX 7.01

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

brk(2)

malloc(3X)

MALLOC(3C)

NAME

malloc, calloc, realloc, free − main memory allocator

SYNOPSIS

#include <stdlib.h>

void ∗malloc (size)
size_t size;

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

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

void free (ptr)
void ∗ptr;

DESCRIPTION

The set of malloc functions provide a simple, general-purpose memory allocation package. 

Malloc allocates space for a block of at least size bytes; the space is not initialized. 

Calloc allocates space for an array of nelem elements, each of size elsize bytes; the space is initialized to zeros.

Realloc changes the size of the block pointed to by ptr, a pointer to a block previously allocated by malloc, calloc, or realloc, to size bytes.  The contents will be unchanged up to the lesser of the new and old sizes.  If no free block of size bytes is available, realloc will call malloc to allocate a block of size bytes and will then move the data to the new space.  If ptr is a NULL pointer, realloc behaves as malloc(size).  If size is zero and ptr is not a NULL pointer, realloc behaves as free(ptr). 

Free deallocates the space pointed to by ptr, a pointer to a block previously allocated by malloc, calloc, or realloc; the space is made available for further allocation, but its contents are left undisturbed. If ptr is a NULL pointer, no action occurs. 

RETURN VALUE

Malloc, calloc, and realloc return a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object. 

DIAGNOSTICS

Any error condition listed for brk(2) is considered to mean no available memory. Malloc, realloc and calloc return a NULL pointer if there is no available memory or if the memory being managed by malloc has been detectably corrupted.  If this happens, the block pointed to by ptr may be destroyed. 

WARNINGS

Results are undefined if the space assigned by the allocation functions is overrun. 

Sbrk (see brk(2)) is called, as needed, to get memory from the system.

Free and realloc do not check their pointer argument for validity. 

Allocation time is proportional to the number of allocated but un-freed objects.  If a program allocates but never frees, each successive allocation takes longer.  For an alternate, more flexible implementation, see malloc(3X).

SEE ALSO

brk(2), malloc(3X). 

Hewlett-Packard Company  —  HP-UX Release 7.0: Sept 1989

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