C Notes
Complete guide to realloc() in C programming — syntax, resizing allocated memory, handling reallocation failures safely, shrinking and growing memory blocks, practical examples with output.
Real programs rarely know the exact amount of memory they'll need upfront. You might start with space for 10 items, then need room for 50. That's exactly what realloc() is for — it lets you resize a previously allocated memory block without losing the data already stored in it.
Syntax of realloc()
void *realloc(void *ptr, size_t new_size);| Parameter | Description |
|---|---|
ptr | Pointer to previously allocated memory (from malloc/calloc/realloc) |
new_size | New size in bytes |
| Return value | Pointer to resized block (may differ from ptr), or NULL on failure |
How realloc() Works Internally
When you call realloc(), the memory allocator tries one of three strategies:
| Case 1: Enough space after the current block | Expand in place |
| Case 2: No space after | Allocate new block, copy data, free old |
| OLD: ┌─────┬─────┬─────┐ | freed |
| NEW | ┌─────┬─────┬─────┬─────┬─────┬─────┐ |
| Case 3: Failure | Returns NULL, original block is UNCHANGED |
Basic realloc() Example
Before realloc: arr[0] = 10 arr[1] = 20 arr[2] = 30 After realloc: arr[0] = 10 arr[1] = 20 arr[2] = 30 arr[3] = 40 arr[4] = 50 arr[5] = 60
The Critical Mistake: Direct Assignment
Never assign the result of realloc() directly to the original pointer:
// DANGEROUS - if realloc fails, you lose access to original memory!
arr = (int *)realloc(arr, new_size * sizeof(int));
// If this returns NULL, arr is now NULL and the old memory is leaked!Always use a temporary pointer:
// SAFE pattern
int *temp = (int *)realloc(arr, new_size * sizeof(int));
if (temp == NULL) {
// Handle error — arr still points to original data
free(arr);
return 1;
}
arr = temp; // Safe to update only after confirming successSpecial Cases of realloc()
// Case 1: ptr is NULL → behaves like malloc()
int *p = (int *)realloc(NULL, 5 * sizeof(int));
// Equivalent to: int *p = (int *)malloc(5 * sizeof(int));
// Case 2: new_size is 0 → implementation-defined (may free the memory)
realloc(ptr, 0);
// DON'T rely on this — use free() explicitly instead
// Case 3: Shrinking the block
int *arr = (int *)malloc(100 * sizeof(int));
arr = (int *)realloc(arr, 50 * sizeof(int)); // Shrink to 50 ints
// Data in first 50 elements is preservedShrinking Memory with realloc()
Original (10 elements): 5 10 15 20 25 30 35 40 45 50 After shrink (5 elements): 5 10 15 20 25
Practical Example: Growing an Array Dynamically
Enter numbers (-1 to stop): 5 [Resized to capacity: 4] 10 15 20 25 [Resized to capacity: 8] 30 -1 You entered 6 numbers: 5 10 15 20 25 30
realloc() Behavior Summary Table
| Scenario | Behavior |
|---|---|
realloc(NULL, size) | Same as malloc(size) |
realloc(ptr, 0) | Implementation-defined; don't rely on it |
realloc(ptr, larger) | Grows block; new bytes are uninitialized |
realloc(ptr, smaller) | Shrinks block; excess data lost |
| Failure | Returns NULL; original block unchanged |
| Success (moved) | Old pointer is invalid; use new one |
Interview Questions
Q1: What happens to the original pointer after a successful realloc()?
If realloc() moves the data to a new location, the original pointer becomes invalid (dangling). You must use the new pointer returned by realloc(). Accessing the old pointer is undefined behavior.
Q2: Why should you never write ptr = realloc(ptr, new_size) directly?
If realloc() fails and returns NULL, you overwrite your only reference to the original memory block. This causes a memory leak because you can no longer free the original allocation. Always use a temporary variable.
Q3: Does realloc() preserve the original data?
Yes, up to the minimum of old size and new size. If you grow the block, existing data is preserved and new bytes are uninitialized. If you shrink, data beyond the new size is lost.
Q4: Can realloc() be used with calloc()-allocated memory?
Yes. realloc() works with any pointer returned by malloc(), calloc(), or a previous realloc(). However, the newly added bytes from realloc() are NOT zero-initialized (unlike calloc).
Summary
realloc()resizes a previously allocated memory block- It may move the data to a new location if the block can't be expanded in place
- Always use a temporary pointer to handle failure safely
- New bytes added when growing are uninitialized (garbage values)
realloc(NULL, size)behaves likemalloc(size)- This is the foundation for implementing growable/dynamic arrays in C
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for realloc() in C – Resizing Dynamic Memory.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, dynamic, memory, allocation, realloc, realloc() in c – resizing dynamic memory
Related C Programming Topics