Introduction
Memory reallocation with realloc() is essential in C programming when handling dynamic data. It allows a program to resize memory blocks at runtime, enhancing performance and flexibility.
Why Study This Topic?
Efficient memory usage is critical in systems programming, game development, and embedded systems. Imagine a photo-editing app that lets you add more layers or undo steps—realloc()
allows programs to grow or shrink memory as needed without crashing or restarting. Mastering this helps you build scalable and efficient applications.
What Will Be Covered?
- Purpose and syntax of
realloc()
- Differences between
malloc()
,calloc()
, andrealloc()
- Use cases with practical C code examples
- Common mistakes and best practices
- Diagram of memory reallocation flow
- Interview questions and quizzes
What is realloc()
in C?
realloc()
(short for reallocate) is a standard library function that resizes a previously allocated memory block using malloc()
or calloc()
.
void* realloc(void* ptr, size_t new_size);
ptr
: pointer to the previously allocated memory blocknew_size
: the new size in bytes
Basic Example of realloc()
#include <stdio.h> #include <stdlib.h> int main() { int* numbers = (int*) malloc(2 * sizeof(int)); numbers[0] = 10; numbers[1] = 20; numbers = (int*) realloc(numbers, 4 * sizeof(int)); numbers[2] = 30; numbers[3] = 40; for (int i = 0; i < 4; i++) { printf("%d ", numbers[i]); } free(numbers); return 0; }
Common Mistakes with realloc()
- Not checking for NULL after reallocation
- Memory leak if the original pointer is overwritten without backup
- Using
realloc()
on uninitialized or freed pointers
Best Practices
- Always assign
realloc()
to a temporary pointer - Check if the return value is not NULL
- Free memory after usage
int* temp = realloc(numbers, new_size); if (temp != NULL) { numbers = temp; }
Summary
realloc()
resizes memory blocks dynamically- Useful for growing arrays or structures
- Prevents reallocation from scratch
- Must be handled with care to avoid memory leaks
Learning Outcomes
After completing this topic, learners will:
- Implement memory reallocation with realloc()
- Write programs that adapt to changing data size
- Avoid common pitfalls like memory leaks and dangling pointers
Common Interview Questions
Q1. What is the purpose of realloc()
in C?
A: To resize memory previously allocated using malloc()
or calloc()
Asked in: Infosys, TCS
Q2. What happens if realloc()
fails?
A: It returns NULL and the original memory block remains unchanged
Asked in: Wipro, Accenture
Q3. Can realloc()
shrink a memory block?
A: Yes, it can shrink or expand memory
Asked in: Capgemini, Cognizant
Q4. Should you always assign realloc()
result to the original pointer?
A: No, always use a temporary pointer to avoid memory leaks
Asked in: IBM, Mindtree
Q5. Is it safe to use realloc()
on a NULL pointer?
A: Yes, it works like malloc()
in that case
Asked in: HCL, Tech Mahindra
Practice Exercises
Coding Task
Write a C program that takes an initial array of 3 elements, then uses realloc()
to increase it to 6 elements and prints all values.
Scenario
Imagine you’re organizing a birthday party. Initially, you planned for 10 guests and set 10 chairs. Suddenly, 5 more guests arrive. Instead of starting over, you simply add 5 more chairs to the table. That’s exactly how realloc()
helps your program handle more data without resetting the whole memory setup.
Now Paly Time :
Additional Resources
- Book: Let Us C by Yashavant Kanetkar
- Article: Memory allocation realloc()
- Tool: GCC Compiler
- IDEs: Code::Blocks, DevC++, Visual Studio Code