realloc<ALLOC.H>
realloc reallocates main memory
Declaration:
void far *farrealloc(void far *oldblock, unsigned long nbytes);
Remarks:
realloc adjusts the size of the allocated block to size, copying the
contents to a new location if necessary.
Return Value:
On success, both functions return the address of the reallocated block,
which might be different than the address of the original block.
On failure (if the block can't be reallocated, or if size == 0 for
realloc), the functions return null.
#include <stdio.h>
#include <alloc.h>
#include <string.h>
int main(void)
{
char *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;
}
0 comments:
Post a Comment