C Function Realloc
# C Library Function - realloc()
[ C Standard Library - ](#)
realloc() is a function in the C standard library used to reallocate memory space. It is very important in dynamic memory management, especially when the size of already allocated memory needs to be adjusted.
The C library function **void *realloc(void *ptr, size_t size)** attempts to resize the memory block pointed to by **ptr**, which was previously allocated by a call to **malloc** or **calloc**.
### Declaration
Here is the declaration of the realloc() function:
void *realloc(void *ptr, size_t size)
### Parameters
* **ptr** -- A pointer to a memory block to be reallocated, which was previously allocated by calling malloc, calloc, or realloc. If it is a null pointer, a new memory block is allocated, and a pointer to it is returned.
* **size** -- The new size of the memory block, in bytes. If the size is 0 and ptr points to an existing memory block, the memory block pointed to by ptr is freed, and a null pointer is returned.
### Return Value
* If successful, `realloc()` returns a pointer to the new memory block.
* If it fails, it returns `NULL`, and the original memory block remains unchanged (it is not freed).
### Usage Notes
* `realloc()` may move the memory block to a new location (if there is not enough space at the original location to accommodate the new size). If the move is successful, `ptr` will point to the new location. It is important to note that the old `ptr` pointer needs to be updated to the new address returned by `realloc()`.
* If the memory allocation fails, `realloc()` returns `NULL`, and the original memory block is not freed. To avoid memory leaks, you should use a temporary pointer to receive the return value of `realloc()` and check if it is `NULL`.
## Examples
The following examples demonstrate the usage of the realloc() function.
## Example 1
#include#include#includeint main(){char *str; /* Initial memory allocation */str = (char *)malloc(15); strcpy(str, ""); printf("String = %s, Address = %pn", str, str); /* Reallocate memory */str = (char *)realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %pn", str, str); free(str); return(0); }
Let us compile and run the above program, which will produce the following result:
String = , Address = 0x7fa2f8c02b10String = .com, Address = 0x7fa2f8c02b10
## Example 2
#include
#include
int main(){
int*arr =malloc(5*sizeof(int));// Allocate memory for 5 integers
if(arr == NULL){
printf("Memory allocation failedn");
return 1;
}
// Fill the array
for(int i =0; i <5; i++){
arr= i *2;
}
// Reallocate memory, resize to 10 integers
int*new_arr =realloc(arr,10*sizeof(int));
if(new_arr == NULL){
printf("Reallocation failedn");
free(arr);// Free original memory on failure
return 1;
}
// Fill the new part
for(int i =5; i <10; i++){
new_arr= i *2;
}
// Print the new array
for(int i =0; i <10; i++){
printf("%d ", new_arr);
}
free(new_arr);// Finally, free the memory
return 0;
}
### Important Notes
* **Check the Return Value**: Always check the return value of `realloc()`. If it returns `NULL`, do not discard the original pointer directly.
* **Memory Leak**: If `realloc()` returns `NULL`, the original memory block is not freed. In this case, you should free the original memory and handle the error condition.
* **Pointer Invalidation**: If you use the original pointer directly to receive the new pointer returned by `realloc()`, it may lead to memory leaks, especially if `realloc()` fails. Therefore, it is common practice to first use a temporary pointer to receive the return value.
### Common Uses
* Dynamically expanding the size of arrays or other data structures.
* Handling variable-sized data sets at runtime, such as processing dynamically generated lists, strings, or buffers.
[ C Standard Library - ](#)
YouTip