Dynamic Memory Allocation in C – Explained with Examples
Dynamic Memory Allocation in C – Explained with Examples
Dynamic memory allocation is one of the most important concepts in C programming. Unlike static memory (like arrays), dynamic memory allows you to request memory from the heap during runtime. This gives your programs more flexibility and efficiency.
π What is Dynamic Memory Allocation?
In C, memory is divided into three segments:
- Stack: Used for static memory like local variables
- Heap: Used for dynamic memory allocation
- Code & Data Segment: For instructions and global/static variables
When we use functions like malloc()
, calloc()
, or realloc()
, we are asking the operating system to give us memory from the heap, which we can use at runtime.
π ️ Why Use Dynamic Memory?
- When you don’t know the required size in advance
- To manage large data structures like linked lists, trees, graphs
- To allocate memory only when needed, saving RAM
π§ The Key Functions
malloc()
– Allocates uninitialized memorycalloc()
– Allocates zero-initialized memoryrealloc()
– Resizes previously allocated memoryfree()
– Frees the allocated memory
π Example 1: malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
// Dynamically allocate memory
ptr = (int*) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
return 1;
}
printf("Memory successfully allocated using malloc.\n");
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
printf("The array is: ");
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
π Example 2: calloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5;
ptr = (int*) calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
return 1;
}
printf("Memory successfully allocated using calloc.\n");
for (int i = 0; i < n; i++) {
printf("%d ", ptr[i]); // All values initialized to 0
}
free(ptr);
return 0;
}
π Example 3: realloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 3;
ptr = (int*) malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
ptr[i] = i + 1;
}
// Resize memory
ptr = (int*) realloc(ptr, 5 * sizeof(int));
for (int i = 3; i < 5; i++) {
ptr[i] = i + 1;
}
printf("Reallocated array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
free(ptr);
return 0;
}
π£ Common Mistakes
- Forgetting to
free()
memory → causes memory leaks - Accessing memory after
free()
- Not checking if
malloc()
returned NULL
π Memory Layout (Simple Diagram)
+--------------+ | Stack | → Local Variables +--------------+ | Heap | → malloc(), calloc(), realloc() +--------------+ | Static/Global | +--------------+ | Code | +--------------+
π Summary
- malloc – Allocates memory but leaves it uninitialized
- calloc – Allocates and zeroes the memory
- realloc – Resizes memory previously allocated
- free – Always free memory to avoid leaks
Understanding dynamic memory is fundamental in systems programming, data structures, and performance-critical applications. Mastering it means writing more efficient, scalable, and professional C programs.
π§ Practice Challenge
Write a program that dynamically allocates an array of n integers, reads the values, and finds the maximum number. Try using both malloc()
and calloc()
versions.
Comments
Post a Comment