realloc() Reallocate Memory Block
#include <malloc.h> Required for declarations only
char *realloc(ptr,size);
char *ptr; Pointer to allocated memory block
unsigned size; New size in bytes
realloc() changes the size of a previously allocated memory block.
'ptr' is the address of the block being resized, and 'size' is the
new size in bytes. The contents of the block are unchanged up to the
shorter of the new and old sizes.
Returns: Pointer to the reallocated block. If the block cannot be
reallocated, then NULL is returned and the original block
is freed.
-------------------------------- Example ---------------------------------
The following statements allocate 500 bytes and then reallocate the
block to 1000 bytes; if the reallocation fails, the original memory
block is reallocated to the original size:
#include <malloc.h>
#include <stdio.h> /* for printf and NULL */
char *memptr, *temp;
main()
{
if ((memptr = malloc(500)) == NULL)
printf("not enough room to allocate memory\n");
else {
.
/* assign values in '*memptr' block */
.
if ((temp = realloc(memptr, 1000)) == NULL) {
printf("not enough room to reallocate memory\n");
memptr = realloc(memptr, _msize(memptr));
}
else
memptr = temp;
/* assigned values in '*memptr' block are still valid */
}
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster