Written: January 13, 2026
Intro
In my first year, I learnt C language, and have used malloc() multiple times in a program. I was curious about what work it actually did under the hood and how did it actually allocate memory. So I did some research, and watched a couple of videos on youtube.. this was one of the videos that was very useful.
The Heap: what does malloc() do? - bin 0x14 by LiveOverflow
This post documents everything that I have learnt so far. This is a work-in-progress, I will continue updating this post as i get more knowledge about this topic.
What is Malloc()
malloc() is a C function that is used to allocate memory for a program.
You can access the system heap using the inbuilt C function: malloc().
It tries to give your program a chunk of memory that is large enough to hold size bits.
Concepts
Memory is the address space that is in the RAM or swap.
Accessing Heaps in C
Heaps can be accessed in C using
-
sbrk()-> to access main heap -
mmap()-> to access the additional heap
Note:
- when you request a memory block of 100 bytes, malloc recieves memory slightly above 100 bytes, due the the extra headers stored
- when memory is freed, it does not go back to OS directly -- it is stored in bins
Why brk() and mmap() aren't used directly in the program instead of malloc()
What are they?
brk() --> low level system call in Linux systems that changes size of heap by moving program break(the boundary marking end of process data seg)
mmap() --> system call in Linux that creates memory by mapping files or devices directly to processes address space.
This allows you to access file contents as if it were regular memory.
Advantage of mmap: it on freeing, memory is immediately returned to the kernel.
Why not use them directly?
-
brk()limitation -- only creates extra space at the end of the heap. if you free a large block in the middle, then it remains locked until everything is freed. -
mmap()limitation -- limited number of mappings (kernel limits number of seperate mmap() regions that you can have.)
Malloc's advantage: Malloc is an inbuilt function within C++ that intelligently chooses between brk() and mmap() based on allocation size.
- if size req is small --> uses
brk() - if size req is big --> uses
mmap()
How malloc works
Heap: fixed lump of mapped memory
malloc() also helps organize and manage heap. It returns the address within the heap that contains the free lump of memory of required size.
But how does malloc know which memory is free??
Method1- DLmalloc()
Algorithm:
For each chunk assigned, it stores a header containing the chunk size. If a chunk is free, it uses a footer (boundary tag) at the end and pointers inside the body to link it to other free chunks
What I'm still learning:
- How the bin system works in detail
- Other malloc implementations
- How to create a basic allocator by myself?

Top comments (0)