Malloc vs Realloc in C: Key Differences and Usage
malloc allocates a new block of memory of a specified size, while realloc resizes an existing memory block to a new size, possibly moving it. Use malloc to get fresh memory and realloc to grow or shrink an existing allocation.Quick Comparison
Here is a quick side-by-side comparison of malloc and realloc functions in C.
| Aspect | malloc | realloc |
|---|---|---|
| Purpose | Allocate new memory block | Resize existing memory block |
| Input | Size in bytes | Pointer to old block and new size |
| Returns | Pointer to new memory or NULL | Pointer to resized memory or NULL |
| Memory content | Uninitialized | Preserves old content up to min(old,new) size |
| Can move memory? | No | Yes, may move to new location |
| Use case | Initial allocation | Change size of allocated memory |
Key Differences
malloc is used to allocate a fresh block of memory of a given size. It returns a pointer to the start of this block, which contains uninitialized data. If the allocation fails, it returns NULL. You use malloc when you need new memory for your program.
realloc takes a pointer to an existing memory block and a new size. It tries to resize that block to the new size. If there is enough space, it keeps the block in place and adjusts its size. If not, it allocates a new block, copies the old data up to the smaller of the old and new sizes, frees the old block, and returns the new pointer. If it fails, it returns NULL but does not free the old block.
In short, malloc creates new memory, while realloc changes the size of memory you already have, preserving data when possible.
Code Comparison
This example shows how to allocate memory for an integer array using malloc and initialize it.
#include <stdio.h> #include <stdlib.h> int main() { int *arr = (int *)malloc(3 * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed\n"); return 1; } arr[0] = 10; arr[1] = 20; arr[2] = 30; for (int i = 0; i < 3; i++) { printf("%d ", arr[i]); } printf("\n"); free(arr); return 0; }
Realloc Equivalent
This example starts with a small array allocated by malloc and then uses realloc to resize it to hold more integers, preserving the old values.
#include <stdio.h> #include <stdlib.h> int main() { int *arr = (int *)malloc(3 * sizeof(int)); if (arr == NULL) { printf("Initial allocation failed\n"); return 1; } arr[0] = 10; arr[1] = 20; arr[2] = 30; int *temp = (int *)realloc(arr, 5 * sizeof(int)); if (temp == NULL) { printf("Reallocation failed\n"); free(arr); return 1; } arr = temp; arr[3] = 40; arr[4] = 50; for (int i = 0; i < 5; i++) { printf("%d ", arr[i]); } printf("\n"); free(arr); return 0; }
When to Use Which
Choose malloc when you need to allocate memory for the first time and do not have an existing block. It is simple and straightforward for initial memory needs.
Choose realloc when you already have allocated memory but need to change its size, either to grow or shrink it, while preserving the existing data. This avoids manual copying and freeing.
In summary, use malloc for new allocations and realloc for resizing existing allocations efficiently.
Key Takeaways
malloc allocates new memory; realloc resizes existing memory.realloc preserves old data up to the smaller size when resizing.malloc for first-time allocation and realloc to grow or shrink memory.NULL to handle allocation failures safely.realloc may move memory, so always assign its result back to your pointer.