0Pricing
C Academy · Lesson

A Simple Bump Allocator

Hand out memory linearly.

A Simple Bump Allocator is a free C Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Bump Allocator Idea

A bump (or arena) allocator is the simplest design. You keep one big buffer and a single offset. Each allocation just returns the current offset, then "bumps" the offset forward by the requested size.

There is no per-block metadata and no search. Allocation is essentially one pointer addition, making it extremely fast.

A Static Backing Buffer

For a self-contained example we back the allocator with a static array instead of the OS heap. This compiles and runs anywhere, with no sbrk or mmap.

The array gives us a fixed pool of bytes to carve up.

#define POOL_SIZE 1024
static unsigned char pool[POOL_SIZE];
static size_t offset = 0;

The Core bump Function

Allocation checks if enough room remains, records the start, advances the offset, and returns the start pointer. If the request would overflow the pool, it returns NULL.

That overflow check is the only safety the bump allocator provides.

void *bump_alloc(size_t size) {
    if (offset + size > POOL_SIZE)
        return NULL;            /* out of pool */
    void *p = &pool[offset];
    offset += size;
    return p;
}

A Complete Runnable Bump Allocator

Here is a full program. It allocates two integers and a short string from the pool and prints them, proving the allocator works.

Notice how little code it takes compared to a real malloc.

#include <stdio.h>
#include <stddef.h>
#include <string.h>

#define POOL_SIZE 1024
static unsigned char pool[POOL_SIZE];
static size_t offset = 0;

void *bump_alloc(size_t size) {
    if (offset + size > POOL_SIZE) return NULL;
    void *p = &pool[offset];
    offset += size;
    return p;
}

int main(void) {
    int *a = bump_alloc(sizeof(int));
    int *b = bump_alloc(sizeof(int));
    char *s = bump_alloc(6);
    *a = 10; *b = 32;
    strcpy(s, "hi");
    printf("%d %d %s\n", *a, *b, s);
    printf("used = %zu\n", offset);
    return 0;
}

No Individual Free

The catch: a bump allocator cannot free a single allocation. Since there is no metadata, it has no idea where one block ends and the next begins for reuse.

You can only reset the entire arena at once by setting the offset back to zero.

void bump_reset(void) {
    offset = 0;   /* frees everything at once */
}

Why Resetting Is Useful

This all-or-nothing model is perfect for phase-based work: allocate many objects during a request or frame, then reset the arena when the phase ends.

Game engines and compilers use arenas heavily because resetting is O(1) and avoids tracking thousands of individual frees.

/* Per-frame pattern */
for (int frame = 0; frame < 3; frame++) {
    void *tmp = bump_alloc(128);
    /* ... use tmp this frame ... */
    bump_reset();   /* reclaim instantly */
}

Tracking Remaining Space

It is handy to expose how much room is left. That is simply the pool size minus the current offset.

Callers can use this to decide whether to flush or grow before requesting more.

size_t bump_remaining(void) {
    return POOL_SIZE - offset;
}

A Runnable Reset Demo

This program fills part of the pool, prints usage, resets, and shows the offset returning to zero so the space is reusable.

#include <stdio.h>
#include <stddef.h>

#define POOL_SIZE 256
static unsigned char pool[POOL_SIZE];
static size_t offset = 0;

void *bump_alloc(size_t s){ if(offset+s>POOL_SIZE) return NULL; void *p=&pool[offset]; offset+=s; return p; }
void bump_reset(void){ offset = 0; }

int main(void) {
    bump_alloc(100);
    printf("after alloc: used=%zu\n", offset);
    bump_reset();
    printf("after reset: used=%zu\n", offset);
    return 0;
}

Alignment in a Bump Allocator

Raw byte-by-byte bumping can return misaligned pointers. To be safe, round the offset up to an alignment boundary before returning a pointer.

We cover the math in detail later, but the bump allocator is where alignment matters most because there is no padding otherwise.

static size_t align_up(size_t n, size_t a) {
    return (n + a - 1) & ~(a - 1);   /* a must be power of 2 */
}

An Aligned Bump Allocator

Combining the pieces, we align the offset before each allocation. This guarantees every returned pointer is suitable for any common type.

The cost is a little internal fragmentation from the padding bytes.

#define ALIGN 16
void *bump_aligned(size_t size) {
    offset = align_up(offset, ALIGN);
    if (offset + size > POOL_SIZE) return NULL;
    void *p = &pool[offset];
    offset += size;
    return p;
}

Strengths and Limits

Bump allocators are unbeatably fast and trivially simple, with zero per-object overhead. They are ideal when objects share a lifetime.

Their weakness is the lack of fine-grained free. When lifetimes differ, you need the free-list design covered in the next lesson.

Quick Check

Consider how a bump allocator reclaims memory.

Recap

A bump allocator hands out memory by advancing one offset through a buffer, making allocation as cheap as a pointer add.

It trades away individual freeing for speed and simplicity, reclaiming memory only via a full reset. Align the offset to keep returned pointers valid for all types.

Frequently asked questions

Is the “A Simple Bump Allocator” lesson free?

Yes — the full text of “A Simple Bump Allocator” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “A Simple Bump Allocator”?

Hand out memory linearly. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “A Simple Bump Allocator” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. How malloc Works
  2. A Simple Bump Allocator
  3. Free Lists and Reuse
  4. Alignment and Splitting
← Back to C Academy