Veda Bites are swipeable micro-lessons — each one teaches exactly one
idea. The bites below come from this kit — swipe through them in the app.
💡 Key Idea
Memory on Demand: The Heap at Runtime
Your program can ask for memory while running.
Dynamic memory allocation lets a C program request memory at runtime from a region called the heap, instead of fixing array sizes at compile time.
↳ Dynamic allocation trades compile-time certainty for runtime flexibility — and you become responsible for cleanup.
📖 Definition
malloc and free: The Core Pair
Two functions rule the heap.
Every `malloc` should have a matching `free` somewhere in your program.
↳ malloc gives memory; free takes it back — they are a matched pair.
💻 Code Example
Your First malloc and free
Five lines that allocate and release.
↳ Allocate, check, use, free — the four-step malloc pattern.
➗ Formula
Sizing malloc Correctly
Multiply count by element size.
For 10 integers on a typical 4-byte int system: `malloc(10 * sizeof(int))` requests 40 bytes.
↳ Never hardcode byte counts — use sizeof so your code stays portable.
⚠️ Common Mistake
Forgetting to free: The Memory Leak
Lost pointers eat your RAM.
↳ Unfreed memory is not garbage-collected in C — it is lost until the process ends.
⚖️ Comparison
Stack vs Heap Memory
Two places your variables live.
↳ Stack is automatic and fast; heap is manual, larger, and flexible.
⚙️ Process
What malloc Actually Does
Behind the scenes of a malloc call.
↳ malloc returns raw, uninitialized memory — it does not zero it out.
🚫 Warning
malloc Returns Garbage, Not Zeros
Uninitialized memory is a trap.
↳ Always write before you read memory returned by malloc.
⚠️ Common Mistake
Double Free and Dangling Pointers
Freeing twice crashes programs.
↳ One malloc, one free — and null the pointer right after.
✅ Best Practice
Always Check for NULL
malloc can fail — plan for it.
↳ Treat every malloc result as untrusted until you have checked it for NULL.
🌍 Real World
Where malloc Powers Real Software
Linked lists, strings, and more.
↳ Any data structure whose size is unknown at compile time relies on dynamic allocation.
🎯 Exam Tip
Exam-Ready Facts on malloc and free
Score marks with these exact points.
↳ These five facts cover most exam questions on dynamic allocation.
🧠 Memory Trick
Remember the malloc Lifecycle
Allocate, Check, Use, Free.
Think A-C-U-F: Allocate, Check for NULL, Use, Free. Four steps, every time.
↳ A-C-U-F keeps you from the two most common heap bugs.
❓ Did You Know
malloc's Sibling: realloc
Grow a block without losing data.
`realloc(ptr, new_size)` resizes a previously allocated block, copying the old contents if it must move. It is the third member of the malloc family alongside `calloc`.
↳ realloc lets you grow or shrink a heap block while preserving its data.
💡 Use Case
Building a Linked List Node
One malloc per node.
Each node is allocated individually, so the list can grow to any length at runtime.
↳ Dynamic allocation is what makes self-referential structures like linked lists possible.