Dynamic Memory Allocation:
Compile Time: Time when the code is checked for syntax errors before execution.
Run Time: Time when the program actually runs and performs tasks.
Types of Memory Allocation:
1.Static Memory Allocation:
Static Memory Allocation: Memory is allocated at compile time.
Memory size is fixed and cannot be changed during execution.
Done using normal variables or arrays like int a[10];.
Fast access as memory is pre-allocated.
Memory is stored in the stack segment of memory.
No need to use malloc() or free() in static allocation.
Less flexible but simple and efficient for small programs.
2.Dynamic Memory Allocation:
Memory is allocated at run time (while the program is running).
Used when memory size is not known in advance.
Allows flexible and efficient use of memory.
Memory is taken from the heap segment.
Common functions used:
malloc() – allocates memory (uninitialized).
calloc() – allocates memory (initialized to 0).
realloc() – resizes previously allocated memory.
free() – releases allocated memory.
Must manually free memory to prevent memory leaks.
Useful for dynamic data structures like linked lists, stacks, queues, etc.
Slower than static allocation but more powerful and flexible.
Functions in Dynamic Memory Allocation:
Memory is taken from the Heap area (not Stack).
To do this, we use 4 special functions in the stdlib.h library:
✅ Functions for DMA
Function Purpose Values Initialized?
malloc() Allocates raw memory ❌ No (garbage values)
calloc() Allocates memory and sets values to 0 ✅ Yes (all zeros)
realloc() Changes size of previously allocated memory –
free() Releases memory back to system –
🔍 Program Explanation – All DMA Functions
Here's the full code again for reference:
c
CopyEdit
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
int n = 5, i;
// 1️⃣ malloc()
ptr = (int*) malloc(n * sizeof(int));
if(ptr == NULL) {
printf("Memory not allocated using malloc.\n");
return 1;
}
printf("Using malloc (Garbage values):\n");
for(i = 0; i < n; i++) {
printf("%d ", ptr[i]);
}
// 2️⃣ calloc()
int *cptr = (int*) calloc(n, sizeof(int));
if(cptr == NULL) {
printf("\nMemory not allocated using calloc.\n");
return 1;
}
printf("\n\nUsing calloc (All zeros):\n");
for(i = 0; i < n; i++) {
printf("%d ", cptr[i]);
}
// 3️⃣ realloc()
n = 8;
ptr = (int*) realloc(ptr, n * sizeof(int));
if(ptr == NULL) {
printf("\nMemory not reallocated using realloc.\n");
return 1;
}
printf("\n\nAfter realloc (malloc pointer resized to 8 ints):\n");
for(i = 0; i < n; i++) {
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
// 4️⃣ free()
free(ptr);
free(cptr);
printf("\n\nMemory freed successfully.\n");
return 0;
}
🔎 Detailed Explanation (Line-by-line)
✅ Step 1: malloc() — Allocate Memory Without Initialization
c
CopyEdit
ptr = (int*) malloc(n * sizeof(int));
Allocates memory for 5 integers (n = 5).
sizeof(int) is usually 4 bytes.
But values are not initialized → output shows garbage values.
✅ Step 2: calloc() — Allocate and Initialize to 0
c
CopyEdit
cptr = (int*) calloc(n, sizeof(int));
Allocates memory for 5 integers.
All values are automatically set to 0.
Safer than malloc when you need clean memory.
✅ Step 3: realloc() — Resize Existing Memory
c
CopyEdit
ptr = (int*) realloc(ptr, n * sizeof(int));
We changed n to 8.
This increases the malloc block from 5 to 8 integers.
Now we assign new values manually:
c
CopyEdit
ptr[i] = i + 1;
✅ Step 4: free() — Release Memory
c
CopyEdit
free(ptr);
free(cptr);
Always free memory after you're done using it.
Prevents memory leaks (when memory is not released properly).
📌 Output Breakdown (Example)
mathematica
CopyEdit
Using malloc (Garbage values):
132847 0 94341 77832 127451 ← Random numbers
Using calloc (All zeros):
00000 ← Clean zeros
After realloc:
12345678 ← Manual values
Memory freed successfully.
✅ Summary
Function What it Does When to Use It
malloc() Allocates memory, no values set You’ll set values manually
calloc() Allocates and sets values to 0 You want clean, zeroed memory
realloc() Resize memory already allocated You need more or less memory
free() Release memory back to the system Done using the memory