New Generic pointer that can point to any data type
1st Parameter
Pointer to the previously allocated memory block. If `ptr` is `NULL`, the function behaves like `malloc`.
2nd Parameter Type : size_t
Platform-specific unsigned type for array indices and memory sizes.
2nd Parameter
The new size in bytes for the memory block. If `size` is 0 and `ptr` is not `NULL`, the memory block is freed.
Read more about parameters of realloc in parameters section
The reallocfunction in C language Changes the size of the memory block pointed to by ptr.
The realloc function changes the size of the memory block pointed to by ptr to size bytes. The contents of the memory block are preserved up to the lesser of the new and old sizes. If the new size is larger, the value of the newly allocated portion is indeterminate. If ptr is NULL, the call is equivalent to malloc(size). If size is zero and ptr is not NULL, the call is equivalent to free(ptr).
The reallocfunction takes 2
parameters:
•
void * `ptr`: Pointer to the previously allocated memory block. If `ptr` is `NULL`, the function behaves like `malloc`.
•
size_t `size`: The new size in bytes for the memory block. If `size` is 0 and `ptr` is not `NULL`, the memory block is freed.
Resizes the memory block pointed to by `ptr` to the new size specified by `size`. The contents of the memory block are preserved up to the lesser of the old and new sizes. Returns a pointer to the resized memory block or `NULL` if the operation fails. The original block is left untouched if reallocation fails.
The realloc function return value :
Returns a pointer to the newly allocated memory, or NULL if the request fails
The returned pointer may be the same as ptr if the allocation was not moved, or a different pointer if it was moved
Output
This example demonstrates basic usage of realloc(). It allocates memory for 5 integers, initializes them, then reallocates memory for 10 integers, preserving the original data and adding new data.