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%

Dynamic Memory: malloc, free, and the Heap

Lesson 26 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Memory at Runtime

Normal arrays (int arr[10]) have a fixed size that must be decided when you write the code. But what if you are building a text editor and don't know how much the user will type? You need Dynamic Memory Allocation.

Stack vs. Heap

  • The Stack: Where local variables live. It's managed automatically by the CPU but is very small (a few MBs).
  • The Heap: A massive pool of memory (gigabytes) that you can manually request and release while your program is running.

The Four Essential Functions (<stdlib.h>)

1. malloc(size_t size)

"Memory Allocation." It requests a block of a specific size in bytes. It returns a generic pointer (void*) which you should cast to your desired type.

int *ptr = (int*) malloc(10 * sizeof(int));

2. calloc(n, size)

"Contiguous Allocation." Like malloc, but it initializes all bits to zero. This is safer but slightly slower.

3. realloc(ptr, new_size)

Resizes a previously allocated block. If you need more space in your array, realloc will find a bigger spot and move your data there automatically.

4. free(ptr)

The Most Important Function. It releases the memory back to the Heap. If you don't call this, you get a Memory Leak.

The Dynamic Memory Workflow

  1. Allocate: Use malloc or calloc.
  2. Check: Always check if the pointer is NULL. If it is, the computer has run out of RAM!
  3. Use: Use the pointer exactly like an array (ptr[i]).
  4. Free: Call free(ptr) as soon as you're done.

The Dangers of Manual Management

  • Memory Leak: You allocated memory but lost the pointer to it without freeing it. If your program runs for weeks (like a server), it will eventually crash the whole computer.
  • Dangling Pointer: You freed the memory but tried to use the pointer again later.
  • Double Free: You called free() twice on the same address.
The Golden Rule: For every malloc you write, there must be a matching free somewhere in your code. Set your pointer to NULL immediately after freeing it to prevent accidental reuse!