C Notes
Complete guide to free() in C programming — releasing heap memory, dangling pointers, double-free errors, memory leaks, best practices for safe deallocation with practical examples.
Every byte you allocate with malloc(), calloc(), or realloc() must eventually be returned to the system. That's the job of free(). Unlike languages with garbage collectors (Java, Python), C puts the responsibility entirely on you — the programmer.
Failing to free memory causes memory leaks. Freeing memory incorrectly causes crashes and security vulnerabilities. Understanding free() properly is essential for writing robust C programs.
Syntax of free()
void free(void *ptr);| Parameter | Description |
|---|---|
ptr | Pointer to memory previously allocated by malloc/calloc/realloc |
| Return value | None (void) |
How free() Works
| Your data: 10, 20, 30... | USED | |
|---|---|---|
| (available for reuse) | FREE |
After free(), the pointer still holds the old address, but that memory is no longer yours. This creates a dangling pointer.
Basic free() Example
Before free: 0 10 20 30 40 Memory freed successfully.
Dangling Pointers — The Silent Killer
A dangling pointer is a pointer that refers to memory that has already been freed:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int));
*ptr = 42;
printf("Before free: %d\n", *ptr);
free(ptr);
// ptr is now DANGLING — it still points to the freed address
// UNDEFINED BEHAVIOR — might print 42, might crash, might print garbage
// printf("After free: %d\n", *ptr); // DON'T DO THIS!
// SOLUTION: Set to NULL after free
ptr = NULL;
// Now safely check before use
if (ptr != NULL) {
printf("Value: %d\n", *ptr);
} else {
printf("Pointer is NULL — memory was freed.\n");
}
return 0;
}Before free: 42 Pointer is NULL — memory was freed.
Double Free — A Dangerous Bug
Freeing the same memory twice causes undefined behavior — crashes, heap corruption, or security exploits:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int));
*ptr = 100;
free(ptr); // First free — OK
// free(ptr); // Second free — UNDEFINED BEHAVIOR! (crash/corruption)
// PREVENTION: Set to NULL after free
ptr = NULL;
free(ptr); // free(NULL) is safe — does nothing
printf("No crash because free(NULL) is a no-op.\n");
return 0;
}No crash because free(NULL) is a no-op.
free(NULL) is Always Safe
The C standard guarantees that free(NULL) does nothing. This is why setting pointers to NULL after freeing is so powerful — even if you accidentally call free again, it won't crash.
int *ptr = NULL;
free(ptr); // Perfectly safe — no crash, no errorWhat You Cannot Free
Memory Leaks — When You Forget to Free
Program running with leaked memory...
Proper Cleanup Pattern
Created student: Alice Student destroyed cleanly.
Common free() Mistakes
| Mistake | Consequence | Prevention |
|---|---|---|
| Not freeing at all | Memory leak | Always pair alloc/free |
| Double free | Heap corruption, crash | Set ptr = NULL after free |
| Use after free | Undefined behavior | Set ptr = NULL, check before use |
| Freeing stack memory | Crash | Only free malloc/calloc/realloc results |
| Freeing partial pointer | Crash | Always free the original pointer |
| Freeing in wrong order | Dangling pointers in struct | Free inner members first |
Interview Questions
Q1: What happens if you call free() on a pointer not returned by malloc/calloc/realloc?
This is undefined behavior. It may crash immediately, corrupt the heap silently, or appear to work but cause mysterious bugs later. Only pass pointers from dynamic allocation functions to free().
Q2: Is it safe to call free(NULL)?
Yes. The C standard (C89 and later) guarantees that free(NULL) is a no-op — it does nothing and does not crash. This is why setting freed pointers to NULL is a good practice.
Q3: What is a memory leak and how do you prevent it?
A memory leak occurs when dynamically allocated memory is never freed, usually because the pointer to it is lost (goes out of scope, gets overwritten). Prevent leaks by: (1) always pairing each malloc with a free, (2) using cleanup functions for complex structures, (3) using tools like Valgrind to detect leaks.
Q4: What is a dangling pointer?
A dangling pointer is a pointer that refers to memory that has been freed. Accessing a dangling pointer is undefined behavior. Prevent it by setting pointers to NULL immediately after calling free().
Q5: Why should you free inner members before the outer struct?
If you free the outer struct first, you lose access to the pointers stored inside it (the inner members). Those inner allocations become unreachable — a memory leak. Always free from the inside out.
Summary
free()returns heap memory to the system- Always set pointers to NULL after freeing (
ptr = NULL) free(NULL)is safe and guaranteed to do nothing- Never free stack memory, string literals, or partial pointers
- Never use a pointer after freeing it (dangling pointer)
- Never free the same pointer twice (double-free)
- For nested structures, free inner allocations before outer ones
- Use tools like Valgrind to detect leaks and invalid frees
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for free() in C – Releasing 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, free, function
Related C Programming Topics