Instead of just using the normal malloc() and free() functions that come with C, I built my own version from scratch!
This program creates a fixed 10KB array to act as a mini fake heap memory. It uses a Singly Linked List to keep track of this memory. Every time you ask for space, a small "header" node is placed right before your data to store how big the block is and whether it is being used or free.
I built this project to understand what happens behind the scenes when a computer allocates memory. It helped me learn:
- How the Heap Works: Managing memory manually without relying on standard built-in shortcuts.
- Pointer Practice: Getting comfortable with pointer arithmetic and changing data types (like
void*andchar*). - Fixing Wasted Space: Learning how to clean up memory clutter (fragmentation) so the program runs efficiently.
- First-Fit Searching: It loops through the memory list from the start and grabs the very first free spot that is big enough for your data.
- Splitting Blocks: If a free spot is way bigger than what you asked for, the program cuts it in half. One half goes to your data, and the other half stays free so space isn't wasted.
- Merging Blocks (Coalescing): When you free up a pointer, the program automatically looks left and right. If the neighboring blocks are also empty, it merges them back into one big clean block.
-
Open your terminal inside this folder and compile the code: gcc allocator.c -o allocator
-
Run the program: ./allocator
- C Language
- GCC Compiler
- Core Concepts: Pointers, Singly Linked Lists, and Memory Allocation
Shafin Alam GitHub: shafinalam07