C library functions - realloc ()
C standard library - <stdlib.h>
description
C library functionsvoid * realloc (void * ptr, size_t size) size of the memory block to try before calling mallocorcallocreadjust assignedptrpoints.
statement
Here is () statement realloc function.
void *realloc(void *ptr, size_t size)
parameter
- ptr - a pointer to re-allocate memory block of memory before the memory block is allocated memory by calling malloc, calloc or realloc of.If a null pointer is assigned a new block of memory, and the function returns a pointer to it.
- size - The new size of the memory block, in bytes.If the size is 0 and ptr points to an existing memory block, the block of memory pointed to by ptr it is released and returns a null pointer.
return value
This function returns a pointer to the memory reallocation size. If the request fails, it returns NULL.
Examples
The following example demonstrates the realloc () function is used.
#include <stdio.h> #include <stdlib.h> int main() { char *str; /* 最初的内存分配 */ str = (char *) malloc(15); strcpy(str, "w3cschool"); printf("String = %s, Address = %u\n", str, str); /* 重新分配内存 */ str = (char *) realloc(str, 25); strcat(str, ".cc"); printf("String = %s, Address = %u\n", str, str); free(str); return(0); }
Let's compile and run the above program, which will result in the following:
String = w3cschool, Address = 355090448 String = w3cschool.cc, Address = 355090448