<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4" -->

---
title: The Double Headed Stack Allocator | daily.dev
description: A veteran game programmer explains stack (bump) allocators, a technique for fast memory allocation where allocation is a simple integer add and deallocation...
canonical: https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: The Double Headed Stack Allocator | daily.dev
og:description: A veteran game programmer explains stack (bump) allocators, a technique for fast memory allocation where allocation is a simple integer add and deallocation...
og:url: https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4
og:image: https://api.daily.dev/og/posts/HmGTFOcR4.png
og:image:alt: The Double Headed Stack Allocator
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# The Double Headed Stack Allocator

**[Bottom of the sea](https://daily.dev/sources/demofox)** · 8 min read · 0 upvotes · 0 comments

## Summary

A veteran game programmer explains stack (bump) allocators, a technique for fast memory allocation where allocation is a simple integer add and deallocation resets a single index. Drawing on experience from Midway/Surreal Software's animation system and Blizzard's StarCraft 2 team, the post describes the basic single-headed stack allocator, then extends it to a double-headed variant where two allocators grow from opposite ends of a shared memory block to solve fragmentation problems in streaming open-world and shapeshifting character systems. It also covers a pattern for storing variable-sized objects with random access using a paired offset table, and notes these techniques work in multithreaded contexts via atomic increments.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.demofox.org/2026/10/03/the-double-headed-stack-allocator>

## Questions this post answers

### What is a double headed stack allocator and how does it prevent memory fragmentation?

A double headed stack allocator uses one block of memory with two stack pointers (tos values) growing toward each other from opposite ends, one for each independent system, such as level geometry and a character. Each side can reset its own pointer to reallocate without fragmenting the other's memory, since allocations from each side stay contiguous and growth direction is fixed. Out-of-memory occurs when the two pointers cross.

_Engineers designing engine memory systems can find implementation patterns like this one on daily.dev._

### How does a simple stack (bump) allocator implement allocation and deallocation so cheaply?

Allocation is just adding the requested size to an integer top-of-stack (tos) variable and returning the previous tos as the pointer, making it essentially free compared to general-purpose allocators. Deallocation of everything is a single assignment setting tos back to zero. Partial deallocation is possible by remembering and restoring an earlier tos value, useful for recursive allocation patterns.

_Developers chasing allocation hot spots in profilers can track techniques like this via daily.dev._

### How can you store variable sized objects with random access in a fixed memory region using a stack allocator?

Use two stacks from a double headed stack allocator: one stack holds the actual variable-sized object data, while the other stack holds an array of integer offsets pointing to each object's location. A separate counter or a sentinel value, such as all 1s, tracks how many offsets exist, and removing or moving objects just requires updating the offset table.

_Developers designing compact indexed memory layouts can revisit patterns like this through daily.dev._

## Similar posts on daily.dev

- [Why malloc is doing more than I asked for](https://daily.dev/posts/why-malloc-is-doing-more-than-i-asked-for-fjox6za3e) · Hacker News · 6 upvotes · 0 comments

---

Tags: [#performance](https://daily.dev/tags/performance), [#game-development](https://daily.dev/tags/game-development), [#c++](https://daily.dev/tags/c++)

[View this post on daily.dev](https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"The Double Headed Stack Allocator","url":"https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4"},"datePublished":"2026-10-03T14:54:42.668Z","dateModified":"2026-10-03T14:55:04.785Z","description":"A veteran game programmer explains stack (bump) allocators, a technique for fast memory allocation where allocation is a simple integer add and deallocation...","image":"https://media.daily.dev/image/upload/s--1KxV4ohY--/f_auto/v1722860400/public/Placeholder%2007","thumbnailUrl":"https://media.daily.dev/image/upload/s--1KxV4ohY--/f_auto/v1722860400/public/Placeholder%2007","isAccessibleForFree":true,"articleSection":"Bottom of the sea","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Bottom of the sea","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/a3f6624fe4c24a4691dd5be6fd5a548c","url":"https://daily.dev/sources/demofox"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"performance,game-development,c++","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Bottom of the sea","item":"https://daily.dev/sources/demofox"},{"@type":"ListItem","position":3,"name":"The Double Headed Stack Allocator"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/the-double-headed-stack-allocator-hmgtfocr4#faq","mainEntity":[{"@type":"Question","name":"What is a double headed stack allocator and how does it prevent memory fragmentation?","acceptedAnswer":{"@type":"Answer","text":"A double headed stack allocator uses one block of memory with two stack pointers (tos values) growing toward each other from opposite ends, one for each independent system, such as level geometry and a character. Each side can reset its own pointer to reallocate without fragmenting the other's memory, since allocations from each side stay contiguous and growth direction is fixed. Out-of-memory occurs when the two pointers cross. Engineers designing engine memory systems can find implementation patterns like this one on daily.dev."}},{"@type":"Question","name":"How does a simple stack (bump) allocator implement allocation and deallocation so cheaply?","acceptedAnswer":{"@type":"Answer","text":"Allocation is just adding the requested size to an integer top-of-stack (tos) variable and returning the previous tos as the pointer, making it essentially free compared to general-purpose allocators. Deallocation of everything is a single assignment setting tos back to zero. Partial deallocation is possible by remembering and restoring an earlier tos value, useful for recursive allocation patterns. Developers chasing allocation hot spots in profilers can track techniques like this via daily.dev."}},{"@type":"Question","name":"How can you store variable sized objects with random access in a fixed memory region using a stack allocator?","acceptedAnswer":{"@type":"Answer","text":"Use two stacks from a double headed stack allocator: one stack holds the actual variable-sized object data, while the other stack holds an array of integer offsets pointing to each object's location. A separate counter or a sentinel value, such as all 1s, tracks how many offsets exist, and removing or moving objects just requires updating the offset table. Developers designing compact indexed memory layouts can revisit patterns like this through daily.dev."}}]}
```

