Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 2 — Dynamic Memory Allocation

Lesson 29 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Dynamic Memory Allocation in C

Dynamic memory allocation allows programs to request memory at run time rather than compile time. This is managed using functions from stdlib.h.

---

Why Dynamic Allocation?

  • Array size not known at compile time
  • Need to allocate/free memory as needed
  • Efficient use of available memory

---

malloc() — Memory Allocation

Allocates a block of uninitialised memory.

void *malloc(size_t size);
int *arr = (int *)malloc(5 * sizeof(int));
if (arr == NULL) {
    printf("Memory allocation failed!\n");
    exit(1);
}
for (int i = 0; i < 5; i++) arr[i] = i * 10;

---

calloc() — Cleared Allocation

Allocates memory for n elements, all initialised to zero.

void *calloc(size_t n, size_t size);
int *arr = (int *)calloc(5, sizeof(int));
/* arr[0] to arr[4] are all 0 */

---

realloc() — Resize Allocation

Changes the size of previously allocated memory.

void *realloc(void *ptr, size_t new_size);
int *arr = (int *)malloc(5 * sizeof(int));
arr = (int *)realloc(arr, 10 * sizeof(int));  /* expand to 10 ints */

---

free() — Release Memory

Always free dynamically allocated memory to prevent memory leaks.

free(arr);
arr = NULL;   /* good practice: set to NULL after free */

---

Comparison

FunctionInitialisesArgumentsUse case
malloc(n)No (garbage)byte countGeneral allocation
calloc(n, s)Yes (zeros)count + sizeZero-initialised array
realloc(p, n)Partialptr + new sizeResize existing block
free(p)pointerRelease memory

---

Complete Example: Dynamic Array

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n;
    printf("Enter size: ");
    scanf("%d", &n);
    
    int *arr = (int *)malloc(n * sizeof(int));
    if (!arr) { perror("malloc"); return 1; }
    
    for (int i = 0; i < n; i++) {
        printf("arr[%d] = ", i);
        scanf("%d", &arr[i]);
    }
    
    int sum = 0;
    for (int i = 0; i < n; i++) sum += arr[i];
    printf("Sum = %d\n", sum);
    
    free(arr);
    return 0;
}

---

Common Errors

ErrorDescription
Memory leakAllocated but never freed
Dangling pointerUsing memory after free()
Double freeCalling free() twice on same pointer
Buffer overflowWriting beyond allocated bounds