realloc
raw OCR
Reallocates main memory.
Defined in header <stdlib.h>
Syntax
#include <stdlib.h>
void *realloc(void *block, size_t size);
Portability
| DOS | UNIX | Windows | ANSI C | C++ only |
|---|---|---|---|---|
| ■ | ■ | ■ | ■ |
Remarks
realloc attempts to shrink or expand the previously allocated block to size bytes. The block argument points to a memory block previously obtained by calling malloc, calloc, or realloc. If block is a null pointer, realloc works just like malloc.
realloc adjusts the size of the allocated block to size, copying the contents to a new location if necessary.
Return value
realloc returns the address of the reallocated block, which can be different than the address of the original block. If the block cannot be reallocated or size == 0, realloc returns null.
See also
Example
tinelude <stdio.h>
Mnelude <alloc.h>
#inelude <string.h>
int main(void)
{
ehar *str;
/* allocate memory for string */
str = (char *) malloc (10);
/* copy "Hello" into string */
strcpy(str, "Hello");
printf("String is %s\n Address is %p\n", str, str);
str = (char *) realloc (str, 20);
printf ("String is %s\n New address is %p\n", str, str)
/* free memory */
free(str);
return 0;
Differences from modern implementations
Nothing recorded yet.
Modern references
These are search links, not yet checked by hand.
Stable link: /3.1/stdlib.h/realloc/
· short form /3.1/realloc/