Without DMA, C is "static"—you have to decide how much memory you need (like the size of an array) before the program even starts. With DMA, your program can request more memory while it is running.
This is the perfect way to visualize the difference between Static and Dynamic memory. In C, if you don't know the size of your "toy" (data) beforehand, picking the wrong "box" (memory) can cause major problems.
1. Static Allocation (The Permanent Wooden Box)
In Static allocation, you must choose your box size before you start the program. This box is built into the floor; it cannot be changed.
- Problem A (Box too big): You pick a huge box for a tiny marble. You are wasting space in your room that could have been used for other things. (Memory Waste)
- Problem B (Box too small): You pick a small box, but then you try to put a giant teddy bear in it. The bear won't fit, and you can't get a bigger box without stopping everything and rebuilding. (Buffer Overflow/Crash)
Code Example:
int toyBox[10]; // You fixed the size at 10. You can never change this.2. Dynamic Allocation (The Magic Expanding Box)
Dynamic allocation (using malloc and realloc) is like having a magic spandex box that you can stretch or shrink while the program is running.
- If the toy is small: You shrink the box to fit it perfectly so you don't waste space.
- If the toy is big: You use
reallocto stretch the box so the toy fits safely.1. The Core Concept: Stack vs. Heap
To understand DMA, you must know that C memory is divided into two main areas:
- The Stack: Where local variables live. It is fast but fixed in size.
- The Heap: A large pool of free memory. DMA allows you to "rent" space from here.
The Four Key Functions (stdlib.h)
To use DMA, you must include the <stdlib.h> library.
| Function | Purpose | Real-Life Analogy |
malloc() | Allocates a block of memory. | Renting a hotel room. |
calloc() | Allocates multiple blocks and clears them (sets to 0). | Renting a row of clean rooms. |
realloc() | Resizes previously allocated memory. | Moving to a bigger room. |
free() | Releases memory back to the system. | Checking out of the hotel. |
1. malloc() — The "One-Time Rent"
Concept: You request a specific amount of space. You get one big box, but it might contain "trash" (garbage values) from the last person who used it.
1#include <stdio.h>
2#include <stdlib.h>
3
4int main() {
5 // Renting space for 1 integer "Toy"
6 int *box = (int*)malloc(sizeof(int));
7
8 if (box == NULL) return 1; // Check if playroom is full
9
10 *box = 50; // Put toy in
11 printf("Malloc Box Value: %d\n", *box);
12
13 free(box); // Give it back
14 return 0;
15}Output:
Malloc Box Value: 502. calloc() — The "Clean Multi-Pack"
Concept: You request space for multiple items. The manager gives you a row of boxes and cleans them all (sets them to zero) before you touch them.
1#include <stdio.h>
2#include <stdlib.h>
3
4int main() {
5 // Renting 3 clean boxes for 3 Toys
6 int *boxes = (int*)calloc(3, sizeof(int));
7
8 if (boxes == NULL) return 1;
9
10 // Notice we haven't put anything in, yet they are 0
11 printf("Clean Boxes: %d, %d, %d\n", boxes[0], boxes[1], boxes[2]);
12
13 free(boxes);
14 return 0;
15}Output:
Clean Boxes: 0, 0, 03. realloc() — The "Size Adjuster"
Concept: You have a box, but your toy is too big (or you have more toys). You stretch the existing box to a new size without throwing away what's already inside.
1#include <stdio.h>
2#include <stdlib.h>
3
4int main() {
5 int *storage = (int*)malloc(2 * sizeof(int));
6 storage[0] = 10; storage[1] = 20;
7
8 printf("Original Storage: %d, %d\n", storage[0], storage[1]);
9
10 // Toys won't fit! Stretching to hold 4 toys
11 storage = (int*)realloc(storage, 4 * sizeof(int));
12
13 storage[2] = 30; storage[3] = 40;
14 printf("Expanded Storage: %d, %d, %d, %d\n", storage[0], storage[1], storage[2], storage[3]);
15
16 free(storage);
17 return 0;
18}Output:
Original Storage: 10, 20
Expanded Storage: 10, 20, 30, 404. free() — The "Return Policy"
Concept: This is the most important part. If you don't use free(), your program "steals" memory from the computer. Over time, the computer runs out of space.
1#include <stdio.h>
2#include <stdlib.h>
3
4int main() {
5 int *temp = (int*)malloc(1000 * sizeof(int)); // Renting a huge space
6
7 // ... do some work ...
8
9 free(temp); // Returning the space so other programs can use it
10 temp = NULL; // Good practice: point the "key" to nothing so you don't use it by mistake
11
12 printf("Memory returned to the playroom.\n");
13 return 0;
14}Output:
Memory returned to the playroom.Summary Comparison
| Function | Initial Content | Use Case |
malloc | Garbage (Dirty) | When you need space fast and will overwrite it anyway. |
calloc | Zero (Clean) | When you want an array to start empty/at zero. |
realloc | Existing Data | When your data list grows larger than you expected. |
free | N/A | Every time you are done with a malloc or calloc. |
