# Why malloc always does more than I asked for

Original title: Why malloc always does more than I asked for?

> Hacker News story 48977463 · language: en · submitted 2026-07-20
> Web version: https://zeli.app/story/48977463
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 49 · by [nathaah3](<https://news.ycombinator.com/user?id=nathaah3>) · 💬 46
[original](<https://ssenthilnathan3.github.io/blog/malloc/>) · [HN discussion](<https://news.ycombinator.com/item?id=48977463>)

## Summary

I discovered that requesting 13 bytes from malloc triggers complex under-the-hood operations involving headers, back pointers, and padding to ensure proper memory alignment. Building a custom allocator revealed how metadata is essential for freeing individual blocks and how internal fragmentation occurs due to alignment gaps. This journey explains why memory management in C requires more than just moving a cursor forward.

## Key quote

> Those padding bytes between the metadata and my actual object are just sitting there, unused, for the entire lifetime of the allocation.

## HN discussion (46 comments)

### [phire](<https://news.ycombinator.com/item?id=49017825>)

> \> so alloc() doesn’t just need to hand back a pointer. it needs to hand back a pointer that’s correctly aligned for whatever type the caller is about to store there.
>
> Malloc doesn't know the required alignment (because has no idea what the type is, everything is cast through void). So all malloc implementations have a minimum alignment guarantee. Typically 16 bytes these days on x86, as that means even 128bit SSE values will end up aligned by default.
>
> You couldn't go below the sizeof(void ) anyway, the backpointer needs to aligned too.
>
> The padding only happens when you use memalign or aligned_malloc to specify a much larger alignment.

### [CodesInChaos](<https://news.ycombinator.com/item?id=49017951>)

> Your bump allocator suffers from integer overflows turned into buffer overflows when the requested allocation is big enough:
>
> if (a->cursor + size > a->limit) return NULL; // out of memory
>
> I'd rewrite it like this:
>
> if (size > a->limit - a->cursor) return NULL; // out of memory

### [lexicality](<https://news.ycombinator.com/item?id=49018141>)

> I'm a little confused. We start with
>
> [ Header ][ ...variable padding... ][ Back Pointer ][ User Memory ]
>
> ^ always exactly sizeof(void*)
>
> bytes before User Memory,
>
> no matter how much padding
>
> came before it
>
> and then it says we don't need to align the back pointer and we end up with
>
> [ Header ][ Back Pointer ][ Padding ][ User Memory ]
>
> without a clear explanation of how we now get to the back pointer if it's behind the variable alignment.

### [drivebyhooting](<https://news.ycombinator.com/item?id=49017893>)

> Why bother with dynamic padding and a back pointer? That wastes at least 8 bytes.
>
> You might as well always align to 8 bytes and make your header a multiple of 8.

### [pjmlp](<https://news.ycombinator.com/item?id=49018577>)

> Old magazines like The C/C++ Users' Journal and DDJ used to have ads for companies selling malloc()/free() replacement libraries, exactly because a single implementation isn't adequate to all scenarios.

---

## Cite

- Page: https://zeli.app/story/48977463
- Date: 2026-07-20
- Source: ssenthilnathan3.github.io (https://ssenthilnathan3.github.io/blog/malloc/)
- Hacker News: https://news.ycombinator.com/item?id=48977463

## Other languages

- zh: https://zeli.app/zh/story/48977463 · markdown: https://zeli.app/zh/story/48977463.md
- ja: https://zeli.app/ja/story/48977463 · markdown: https://zeli.app/ja/story/48977463.md
- ko: https://zeli.app/ko/story/48977463 · markdown: https://zeli.app/ko/story/48977463.md
- de: https://zeli.app/de/story/48977463 · markdown: https://zeli.app/de/story/48977463.md
- es: https://zeli.app/es/story/48977463 · markdown: https://zeli.app/es/story/48977463.md
