# Write a Dynamic Memory Allocator

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/cross-platform/dynamic-memory-allocator/)
- [Dynamic memory allocation](https://learn.arm.com/learning-paths/cross-platform/dynamic-memory-allocator/1_dynamic_memory_allocation/)
- [Design a dynamic memory allocator](https://learn.arm.com/learning-paths/cross-platform/dynamic-memory-allocator/2_designing_a_dynamic_memory_allocator/)
- [Implement a dynamic memory allocator](https://learn.arm.com/learning-paths/cross-platform/dynamic-memory-allocator/3_implementing_a_dynamic_memory_allocator/)
- [Memory allocation summary](https://learn.arm.com/learning-paths/cross-platform/dynamic-memory-allocator/4_conclusions_further_work/)
- [Next Steps](https://learn.arm.com/learning-paths/cross-platform/dynamic-memory-allocator/_next-steps/)

## About this Learning Path

|  |  |
| --- | --- |
| Skill level: | Introductory |
| Reading time: | 2 hrs |
| Last updated: | 02 Jul 2026 |

|  |  |
| --- | --- |
| Author: | David Spickett , Arm |
| Arm IP: | [Cortex-A](https://support.arm.com/?tab=compute-ip&Product%20Type=Application%20Processors) [Neoverse](https://support.arm.com/?tab=compute-ip&Product%20Type=Infrastructure%20Processors) |
| Tags: | [Performance and Architecture](https://learn.arm.com/tag/performance-and-architecture) [Linux](https://learn.arm.com/tag/linux) [C](https://learn.arm.com/tag/c) [Runbook](https://learn.arm.com/tag/runbook) |

### Who is this for?

This is an introductory topic for software developers learning about dynamic memory allocation for the first time, and who may have used malloc and free in C programming. It also provides a starting point to explore more advanced memory allocation topics.

### What will you learn?

After completing this Learning Path, you'll be able to perform the following tasks:

- Explain how dynamic memory allocation and the C heap works
- Write a simple dynamic memory allocator
- Explain some of the risks of heap allocation in general

### Prerequisites

Before starting, you'll need the following prerequisites:

- Familiarity with C programming, with a good understanding of pointers.
- A Linux machine to run the example code.

### Summary

You’ll design and implement a minimal dynamic memory allocator in C on Linux, showing how heap-style allocation works behind familiar interfaces. First, you’ll define the behavior of `simple_malloc` and `simple_free`, implement them in a small project with `heap.c` and `heap.h`, and exercise them from a provided test program. Then, you’ll contrast dynamic and static allocation, describe failure behavior, and explore practical trade-offs of a simple allocator. You’ll build and run the code, observe allocations and frees in action, and reason about basic risks and limitations of heap usage when integrating allocation into real programs.

### Frequently asked questions

**Which files do I edit to implement the allocator?**

Put the implementation in `heap.c` and the declarations in `heap.h`. The test program that calls your functions is in `main.c`, and `CMakeLists.txt` configures the build.

<summary>How do I confirm the test program calls my `simple_malloc` and `simple_free`?</summary>
Check that `main.c` includes `heap.h` and uses `simple_malloc` and `simple_free`. You can also add temporary logging or assertions in `heap.c` to verify the call flow during a test run.

<summary>What should I check if `simple_malloc` returns `NULL` for a small request?</summary>
Verify the requested size and confirm your allocator is initialized before the first request. Ensure your implementation handles allocation failure paths correctly and that the test code checks for `NULL` before using the returned pointer.

<summary>Do I need to replace the C library `malloc` and `free`?</summary>
No. You’ll use separate functions named `simple_malloc` and `simple_free` and won’t replace the C library allocator.

<summary>How can I sanity-check that `simple_free` works before moving on?</summary>
Run sequences that allocate, free, and then allocate again to see if subsequent requests succeed without errors. Add basic checks or prints in `main.c` and `heap.c` to confirm that block metadata and returned pointers behave as expected.
