# Zalloc, replace malloc and friends with a zig allocator in your c code

**URL:** <https://ziggit.dev/t/zalloc-replace-malloc-and-friends-with-a-zig-allocator-in-your-c-code/11073>\
**Category:** Showcase\
**Tags:** memory-management, c\
**Created:** [July 20, 2025, 4:28pm UTC](https://ziggit.dev/t/zalloc-replace-malloc-and-friends-with-a-zig-allocator-in-your-c-code/11073 "2025-07-20T16:28:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![D-Berg](https://ziggit.dev/user_avatar/ziggit.dev/d-berg/32/4195_2.png) [@D-Berg](https://ziggit.dev/u/D-Berg)\
**Post date:** [July 20, 2025, 4:28pm UTC](https://ziggit.dev/t/zalloc-replace-malloc-and-friends-with-a-zig-allocator-in-your-c-code/11073/1 "2025-07-20T16:28:18Z")

</div>

> **[GitHub - D-Berg/zalloc: Use zig allocators in your c code.](https://github.com/D-Berg/zalloc)**
>
> Use zig allocators in your c code.

This library overwrites malloc, realloc, calloc and free in a c module with functions that call into a zig allocator.  
Perhaps it is a really stupid idea but I thought it would be fun.

---

<div class="post-metadata">

**Author:** ![jeffective](https://ziggit.dev/user_avatar/ziggit.dev/jeffective/32/3116_2.png) [@jeffective](https://ziggit.dev/u/jeffective)\
**Post date:** [July 20, 2025, 5:55pm UTC](https://ziggit.dev/t/zalloc-replace-malloc-and-friends-with-a-zig-allocator-in-your-c-code/11073/2 "2025-07-20T17:55:08Z")

</div>

Not stupid!

> [@Provide my own malloc and free to C dependency?](https://ziggit.dev/t/provide-my-own-malloc-and-free-to-c-dependency/9280):
>
> Is it possible for me to provide my own allocator to a c dependency so I can detect leaks?

This is so crazy, it just might work.

```zig
pub fn infect(mod: *std.Build.Module) void {
    mod.addCMacro("malloc", "zmalloc");
    mod.addCMacro("realloc", "zrealloc");
    mod.addCMacro("calloc", "zcalloc");
    mod.addCMacro("free", "zfree");
}

```

---

<div class="post-metadata">

**Author:** ![ForeverZer0](https://ziggit.dev/user_avatar/ziggit.dev/foreverzer0/32/2443_2.png) [@ForeverZer0](https://ziggit.dev/u/ForeverZer0)\
**Post date:** [July 20, 2025, 6:35pm UTC](https://ziggit.dev/t/zalloc-replace-malloc-and-friends-with-a-zig-allocator-in-your-c-code/11073/3 "2025-07-20T18:35:40Z")

</div>

This is a pretty clever implementation.  
When I was first learning Zig, I was toying around with game-development, which included creating some “Ziggified” GLFW bindings, and I was struggling to come up with a way to implement the custom [`GLFWallocator`](https://www.glfw.org/docs/latest/struct_g_l_f_wallocator.html) structure, simply because I couldn’t think of a simple way to keep track of the allocation sizes without some complicated lookup table, etc.  
Seeing what you did here makes me feel dumb, it seems so obvious now to just prepend the value to the allocated memory. It is a very elegant solution to a problem that I was over-thinking.

---

<div class="post-metadata">

**Author:** ![D-Berg](https://ziggit.dev/user_avatar/ziggit.dev/d-berg/32/4195_2.png) [@D-Berg](https://ziggit.dev/u/D-Berg)\
**Post date:** [July 20, 2025, 7:47pm UTC](https://ziggit.dev/t/zalloc-replace-malloc-and-friends-with-a-zig-allocator-in-your-c-code/11073/4 "2025-07-20T19:47:54Z")

</div>

Nah you’re not dumb. Did the same thing as you did for tree-sitters custom allocator. Stored the info using a hashmap(usize, usize). Wasn’t the best solution but atleast it found a mem leak which I actually for once wasn’t the root cause of (kind of).

---

<div class="post-metadata">

**Author:** ![D-Berg](https://ziggit.dev/user_avatar/ziggit.dev/d-berg/32/4195_2.png) [@D-Berg](https://ziggit.dev/u/D-Berg)\
**Post date:** [July 20, 2025, 7:52pm UTC](https://ziggit.dev/t/zalloc-replace-malloc-and-friends-with-a-zig-allocator-in-your-c-code/11073/5 "2025-07-20T19:52:37Z")

</div>

Thank you! Funny to see a VERY similar solution to mine. Glad I’m not the only one obsessed with controlling memory. Initially just called them by their c names but that screwed things up with threading as zig std lib uses c\_allocator and c\_raw\_allocator (don’t remember the exact names) for creating the threads which causes it to call my free on pointers which was not allocated by my malloc.
