How to Use realloc in C: Syntax, Example, and Tips
In C,
realloc changes the size of a previously allocated memory block. You pass the pointer to the old memory and the new size in bytes, and it returns a pointer to the resized memory or NULL if it fails.Syntax
The realloc function has this syntax:
void *realloc(void *ptr, size_t new_size);
Here:
- ptr is the pointer to the memory block you want to resize. It must come from
malloc,calloc, orrealloc. - new_size is the new size in bytes you want for the memory block.
- The function returns a pointer to the resized memory block, which may be at a new location.
- If
ptrisNULL,reallocbehaves likemalloc. - If
new_sizeis zero, behavior depends on the system but often frees the memory and returnsNULL.
c
void *realloc(void *ptr, size_t new_size);
Example
This example shows how to use realloc to grow an integer array dynamically:
c
#include <stdio.h> #include <stdlib.h> int main() { int *arr = malloc(3 * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed\n"); return 1; } // Initialize array for (int i = 0; i < 3; i++) { arr[i] = i + 1; } // Resize array to hold 5 integers int *temp = realloc(arr, 5 * sizeof(int)); if (temp == NULL) { free(arr); // free old memory if realloc fails printf("Reallocation failed\n"); return 1; } arr = temp; // Add new elements arr[3] = 4; arr[4] = 5; // Print all elements for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } printf("\n"); free(arr); // free memory return 0; }
Output
1 2 3 4 5
Common Pitfalls
Common mistakes when using realloc include:
- Not assigning the result of
reallocto a temporary pointer. Ifreallocfails and returnsNULL, you lose the original pointer and cause a memory leak. - Using the old pointer after
reallocif the memory was moved. - Not checking if
reallocreturnsNULLbefore using the new pointer. - Passing an invalid pointer to
realloc(not frommalloc,calloc, orrealloc).
Example of wrong and right usage:
c
#include <stdlib.h> #include <stdio.h> int main() { int *arr = malloc(2 * sizeof(int)); if (!arr) return 1; // Wrong way: directly assign realloc result int *temp = realloc(arr, 4 * sizeof(int)); if (!temp) { // If realloc fails, original pointer is not lost printf("Realloc failed, original memory still valid\n"); free(arr); return 1; } arr = temp; // Right way: use a temp pointer temp = realloc(arr, 6 * sizeof(int)); if (!temp) { free(arr); // free original memory printf("Realloc failed, original memory freed\n"); return 1; } arr = temp; free(arr); return 0; }
Quick Reference
- Use a temporary pointer to avoid losing original memory on failure.
- Check for
NULLafterreallocbefore using the pointer. - Free memory when no longer needed to avoid leaks.
- Passing
NULLtoreallocacts likemalloc. - Passing size 0 may free memory and return
NULL.
Key Takeaways
Always assign realloc result to a temporary pointer and check for NULL before using it.
realloc resizes memory blocks and may move them to a new location.
Passing NULL to realloc works like malloc; passing zero size may free memory.
Free memory after use to prevent leaks.
Never use the old pointer after realloc if it returns a new address.