realloc

OS/161 Reference Manual

Name

realloc - resize allocated memory

Library

Standard C Library (libc, -lc)

Synopsis

#include <stdlib.h>

void *
realloc(void *ptr, size_t newsize);

Description

realloc attempts to change the size of the memory block pointed to by ptr to newsize, causing the block to shrink or grow as necessary. If the size cannot be changed, a new block is allocated and the contents (up to the lesser of the previous size and newsize) are copied, then the old block is freed.

The size of NULL is treated as 0. If the size is increased, any newly allocated space has undefined contents. If the size is decreased, the space discarded may no longer be accessed. Otherwise the contents of the memory block are preserved.

ptr must be NULL or have been previously returned by malloc, calloc, or realloc.

The alignment and other restrictions described for malloc apply equally to realloc.

Return Values

realloc returns a pointer to the resized memory block. This may not be the same pointer as ptr. If so, the old block is invalidated and ptr becomes invalid.

If the operation cannot be performed at all, NULL is returned and the original block pointed to by ptr is untouched and remains valid.

See Also

calloc, malloc, free