Introduction
Memory Deallocation with free() plays a crucial role in C programming. The free()
function releases dynamically allocated memory when the program no longer needs it. This practice helps maintain memory efficiency and prevents memory leaks that can slow down or crash applications.
Why Study This Topic?
C programmers manage memory manually, unlike languages with automatic garbage collection. While this offers flexibility and efficiency, it also increases responsibility. If you allocate memory with malloc()
or calloc()
but forget to free it, memory leaks occur. In large-scale software or embedded systems, this can degrade performance or crash the system.
Real-time example:
Consider memory as a hotel room you’ve rented. After your stay, you must check out. If you don’t, you continue paying even if you’re not using it. Similarly, if you don’t free allocated memory, the system still considers it in use.
What Will Be Covered?
- Purpose and syntax of
free()
- Best practices and common pitfalls
- Example programs with explanation
- Diagram of memory management flow
- Interview questions and a quiz
- Real-world analogies and exercises
What is free() in C?
The free()
function deallocates memory that was previously allocated with malloc()
, calloc()
, or realloc()
. It is declared in the <stdlib.h>
header file.
Syntax:
void free(void *ptr);
ptr
must be a pointer that points to dynamically allocated memory. Once you call free(ptr);
, the memory becomes available for reuse by the system.
Code Example
#include <stdio.h> #include <stdlib.h> int main() { int *arr = (int *)malloc(5 * sizeof(int)); if (arr == NULL) { printf("Memory allocation failed!\n"); return 1; } for (int i = 0; i < 5; i++) arr[i] = i + 1; printf("Allocated array: "); for (int i = 0; i < 5; i++) printf("%d ", arr[i]); // Deallocate memory free(arr); arr = NULL; // Prevents dangling pointer return 0; }
Best Practices
- Check whether memory allocation returns
NULL
before using the pointer. - Always set the pointer to
NULL
after deallocating it. - Do not call
free()
on memory that was not allocated withmalloc()
,calloc()
, orrealloc()
. - Avoid freeing the same pointer more than once.
Summary
- The
free()
function releases dynamically allocated memory. - Proper use of
free()
prevents memory leaks. - Setting the pointer to
NULL
avoids accessing invalid memory. - Efficient memory management improves performance and reliability.
Learning Outcomes
After learning this topic, you will be able to:
- Implemented Memory Deallocation with free()
- Prevent memory leaks through disciplined coding
- Apply best practices in memory management
- Detect and fix memory-related bugs such as dangling pointers
Common Interview Questions
Q1. What is the purpose of the free()
function in C?
A: It releases dynamically allocated memory back to the system.
Asked in: Infosys, Wipro
Q2. What happens if a program does not call free()
after using malloc()
?
A: The program may leak memory and consume unnecessary system resources.
Asked in: Cognizant, Capgemini
Q3. Can you call free()
more than once on the same pointer?
A: No. Doing so causes undefined behavior.
Asked in: TCS, HCL
Q4. What should you do after calling free()
?
A: Set the pointer to NULL
to avoid dangling references.
Asked in: IBM, Accenture
Q5. Does free()
delete the pointer itself?
A: No. It deallocates the memory block but not the pointer variable.
Asked in: Tech Mahindra, DXC
Practice Exercises
Coding Task
Write a program that:
- Allocates memory for 10 integers using
calloc()
- Stores values from 1 to 10
- Displays the values
- Frees the memory and sets the pointer to
NULL
Solution Code:
#include <stdio.h> #include <stdlib.h> int main() { int *nums = (int *)calloc(10, sizeof(int)); if (nums == NULL) { printf("Memory allocation failed!\n"); return 1; } for (int i = 0; i < 10; i++) nums[i] = i + 1; for (int i = 0; i < 10; i++) printf("%d ", nums[i]); free(nums); nums = NULL; return 0; }
Scenario
Imagine a library where people borrow books (memory blocks). When someone returns a book (calls free()
), the book goes back on the shelf for others. But if no one returns books, shelves remain empty, and new readers can’t borrow anything. This mirrors how memory leaks occur in C when you don’t call free()
after use.
Now Paly Time :
Additional Resources
- Book: Let Us C by Yashavant Kanetkar
- Tool: Valgrind (for memory leak detection)
- Reference Article: Memory Management in C –