HP-UX Reference (11i v2 07/12) - 3 Library Functions A-M (vol 6)

m
malloc(3C) malloc(3C)
NAME
malloc(), alloca(), calloc(), free(), mallinfo(), mallopt(), memorymap(), realloc(), valloc() - main memory allo-
cator
SYNOPSIS
#include <stdlib.h>
void *malloc(size_t size);
void *calloc(size_t nelem, size_t elsize);
void *realloc(void *ptr, size_t size);
void *valloc(size_t size);
void free(void *ptr);
void memorymap(int show_stats);
(Obsoleted)
alloca
#include <alloca.h>
void *alloca(size_t size);
System V Synopsis
#include <malloc.h>
char *malloc(unsigned size);
void free(char *ptr);
char *realloc(char *ptr, unsigned size);
char *calloc(unsigned nelem, unsigned elsize);
int mallopt(int cmd, int value);
struct mallinfo mallinfo(void);
Remarks
The functionality in the old malloc(3X) package has been incorporated into malloc(3C). The library
(
/usr/lib/libmalloc.a
) corresponding to the -lmalloc linker option is now an empty library.
Makefiles that reference this library will continue to work.
Obsolescent Interfaces
memorymap() has been deprecated at HP-UX 11i Version 1 and is now obsolete. The
mal-
linfo()
function is more useful for malloc statistics.
DESCRIPTION
The functions described in this manual entry provide a simple, general purpose memory allocation package:
malloc() Allocates space for a block of at least size bytes, but does not initialize the space.
calloc() Allocates space for an array of nelem elements, each of size elsize bytes, and initial-
izes the space to zeros. Actual amount of space allocated will be at least nelem *
elsize bytes.
realloc() Changes the size of the block pointed to by ptr to size bytes and returns a pointer to
the (possibly moved) block. Existing contents are unchanged up to the lesser of the
new and old sizes. If ptr is a NULL pointer, realloc() behaves like malloc()
for the specified size. If size is zero and ptr is not a NULL pointer, the object it
points to is freed and NULL is returned. realloc() of blocks with special align-
ments, such as those created by valloc() , is not supported.
valloc() Allocates space for a block of at least size bytes starting on a boundary aligned to a
multiple of the value returned by sysconf(__SC_PAGESIZE). This space is unin-
itialized.
free() Deallocates the space pointed to by ptr (a pointer to a block previously allocated by
malloc(), realloc() , calloc(),orvalloc()) and makes the space avail-
able for further allocation. If ptr is a NULL pointer, no action occurs.
720 Hewlett-Packard Company 1 HP-UX 11i Version 2: December 2007 Update