Dynamic Memory Allocation in C: malloc and free: quick revision, 15 bites
All 15 revision bites from this kit, free and with no sign-up.
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.
Takeaway. Dynamic allocation trades compile-time certainty for runtime flexibility — and you become responsible for cleanup.
malloc and free: The Core Pair
Two functions rule the heap.
Every `malloc` should have a matching `free` somewhere in your program.
Takeaway. malloc gives memory; free takes it back — they are a matched pair.
Your First malloc and free
Five lines that allocate and release.
Takeaway. Allocate, check, use, free — the four-step malloc pattern.
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.
Takeaway. Never hardcode byte counts — use sizeof so your code stays portable.
Forgetting to free: The Memory Leak
Lost pointers eat your RAM.
Takeaway. Unfreed memory is not garbage-collected in C — it is lost until the process ends.
Stack vs Heap Memory
Two places your variables live.
Takeaway. Stack is automatic and fast; heap is manual, larger, and flexible.
What malloc Actually Does
Behind the scenes of a malloc call.
Takeaway. malloc returns raw, uninitialized memory — it does not zero it out.
malloc Returns Garbage, Not Zeros
Uninitialized memory is a trap.
Takeaway. Always write before you read memory returned by malloc.
Double Free and Dangling Pointers
Freeing twice crashes programs.
Takeaway. One malloc, one free — and null the pointer right after.
Always Check for NULL
malloc can fail — plan for it.
Takeaway. Treat every malloc result as untrusted until you have checked it for NULL.
Where malloc Powers Real Software
Linked lists, strings, and more.
Takeaway. Any data structure whose size is unknown at compile time relies on dynamic allocation.
Exam-Ready Facts on malloc and free
Score marks with these exact points.
Takeaway. These five facts cover most exam questions on dynamic allocation.
Remember the malloc Lifecycle
Allocate, Check, Use, Free.
Think **A-C-U-F**: **A**llocate, **C**heck for NULL, **U**se, **F**ree. Four steps, every time.
Takeaway. A-C-U-F keeps you from the two most common heap bugs.
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`.
Takeaway. realloc lets you grow or shrink a heap block while preserving its data.
Building a Linked List Node
One malloc per node.
Each node is allocated individually, so the list can grow to any length at runtime.
Takeaway. Dynamic allocation is what makes self-referential structures like linked lists possible.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages