Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

realloc - Alter memory space

&pagelevel(4)&pagelevel

Definition

#include <stdlib.h>

void *realloc(void *p, size_t n);

realloc changes the size of the memory area pointed to by p to n bytes. p must have been
returned by a previous malloc or calloc call.

realloc is part of a C-specific memory management package which internally administers
memory areas that are requested and subsequently freed. Attempts are made to satisfy
new requests by first using areas that are already being managed and only then by the
operating system (cf. garbcoll function).

Return val.

Pointer to the beginning of the modified memory area

if successful.

NULL pointer

if realloc was unable to alter the memory space, e.g. because the
memory space still available is insufficient or because an error occurred.

Notes

If realloc alters the size of a memory area, it may happen that the allocated block is
shifted. In such cases, the contents of the pointer passed as an argument are not identical
with the return value. The contents of the block are preserved up to the minimum of the old
(when enlarging) and new (when reducing) sizes.

If realloc returns the NULL pointer, the block to which p points may have been destroyed!

If p is a NULL pointer, realloc functions like a malloc call for the specified size.

If n has the value 0, realloc returns an unambiguous address which can also be
transferred to free.

Example

The following program fragment first requests memory space for 20 characters and then
extends this area to accept 80 additional characters (i.e. to a total of 100 bytes).

#include <stdlib.h>
char *char_array;
char_array = (char *)malloc(20 * sizeof(char));
    .
    .
char_array = (char *)realloc(char_array, 100 * sizeof(char));

See also

malloc, calloc, free, garbcoll