- Allocating memory at runtime using
malloc()andcalloc() - Resizing allocated blocks with
realloc() - Releasing memory back to the OS using
free() - Building common data structures (arrays, strings, linked lists, stacks, queues, trees, graphs) with dynamic memory
- Writing leak-free, production-style C code
What is Dynamic Memory Allocation?
Dynamic Memory Allocation (DMA) is the process of allocating memory at runtime rather than at compile time. In C, DMA is managed through four standard library functions from <stdlib.h>:
malloc(), calloc(), realloc(), and free().
DMA is essential when the size of data is not known in advance — for example, when reading an arbitrary number of records from a file, building a data structure of unknown length, or creating linked structures like lists and trees.
💡 Quick Definition: DMA gives your program the power to ask the operating system for memory exactly when it needs it, and to return that memory when it's done.
The 9 Practice Assignments
Each assignment below is a full working problem, explained step-by-step with a complete C program and output. Work through them in order — the difficulty increases gradually.
How to Approach Each Assignment
Every DMA program in this section follows the same skeleton. Once you recognize it, the pattern becomes automatic:
The Universal DMA Program Skeleton
#include <stdio.h>
#include <stdlib.h>
int main() {
/* 1. Declare pointers (no size yet) */
int *ptr = NULL;
int n;
/* 2. Ask the user for the size */
printf("Enter number of elements: ");
scanf("%d", &n);
/* 3. Allocate memory at runtime */
ptr = (int *) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
/* 4. Use the memory - read, write, compute */
/* ... your logic here ... */
/* 5. Always free what you allocated */
free(ptr);
ptr = NULL;
return 0;
}
5 checkpoints for every assignment:
- NULL check after every allocation.
malloc()can fail. - Only allocate what you need. Use
n * sizeof(type), not a hardcoded number. - Set pointers to NULL after
free(). Prevents accidental use-after-free. - Free every block exactly once. No leaks, no double frees.
- Match every
mallocwith afree. If a function allocates and returns a pointer, its caller must free it.
🎯 Key Takeaway: DMA problems are not hard because of the logic — they're hard because of lifecycle discipline. Allocate → check → use → free. Follow that order every time and you'll never leak memory.
Frequently Asked Questions About DMA
1. What is the difference between malloc and calloc?
malloc(n * size) allocates a single block of n * size bytes and leaves the content uninitialized. calloc(n, size) allocates n blocks of size bytes and zero-initializes every byte.
2. Why must I check if malloc returns NULL?
If the system is out of memory, malloc returns NULL. Using that pointer without checking causes undefined behavior — often a crash. Always check and handle the failure gracefully.
3. What is a memory leak?
A memory leak happens when you allocate memory with malloc and forget to free it. The program keeps running but the memory is never returned to the OS. In long-running programs, leaks eventually exhaust available memory.
4. Do I need to cast the result of malloc in C?
Not strictly — C allows implicit conversion from void * to any pointer type. However, casting is common and harmless, and it keeps your code compatible with C++ compilers if you ever port it.
💡 Tip: Run your solutions under valgrind or compile with -fsanitize=address to catch leaks and out-of-bounds writes instantly.