__malloc_hook(3)

πŸŽ€ __malloc_hook(3) β€” the tea on malloc debugging variables (DEPRECATED) πŸ’…

okay bestie sit DOWN because this is one of those pages from the Library Functions Manual and it's giving ✨forbidden knowledge✨ β€” it's a whole family of variables that used to let you poke at how malloc does its thing, and now? now they're deprecated. rip bozo. but we still gotta know them because history is important and also because you might run into them in old code and have a moment. 🫢


🌸 NAME 🌸

__malloc_hook, __malloc_initialize_hook, __memalign_hook, __free_hook, __realloc_hook, __after_morecore_hook β€” malloc debugging variables (DEPRECATED) 😭

highkey just a little gang of function-pointer variables whose whole job was helping you debug memory stuff.


πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc) 🧚


✨ SYNOPSIS ✨

#include <malloc.h>

typeof(void *(size_t size, const void *caller))
    *volatile  __malloc_hook;

typeof(void *(void *p, size_t size, const void *caller))
    *volatile  __realloc_hook;

typeof(void *(size_t align, size_t size, const void *caller))
    *volatile  __memalign_hook;

typeof(void *(void *p, const void *caller))
    *volatile  __free_hook;

typeof(void (void))            *__malloc_initialize_hook;
typeof(void (void)) *volatile   __after_mrecore_hook;
bestie remember: that last one is spelled __after_mrecore_hook in the synopsis and __after_morecore_hook everywhere else. that typo is load-bearing, we do not fix it, we simply observe it. πŸ‘€ no cap.

πŸ’– DESCRIPTION πŸ’–

So here's the vibe: the GNU C library lets you modify the behavior of malloc(3), realloc(3), and free(3) by specifying appropriate hook functions. You can use these hooks to help you debug programs that use dynamic memory allocation, for example. πŸ”₯ literally built-in spy gear for your heap.

The variable __malloc_initialize_hook points at a function that is called once when the malloc implementation is initialized. This is a weak variable, so it can be overridden in the application with a definition like the following:

typeof(void (void))  *__malloc_initialize_hook = my_init_hook;

Now the function my_init_hook() can do the initialization of all hooks. slay, one little entry point to set up your whole operation. πŸ‘‘

The four functions pointed to by __malloc_hook, __realloc_hook, __memalign_hook, __free_hook have a prototype like the functions malloc(3), realloc(3), memalign(3), free(3), respectively β€” except that they have a final argument caller that gives the address of the caller of malloc(3), etc. so you literally get handed the receipt of who asked for that memory. iconic behavior ngl. πŸ•΅οΈβ™€οΈ

The variable __after_morecore_hook points at a function that is called each time after sbrk(2) was asked for more memory. 🫢


🌈 STANDARDS 🌈

GNU. πŸ‡¬πŸ‡³ that's it that's the standard.


🚨 NOTES 🚨

the use of these hook functions is not safe in multithreaded programs, and they are now deprecated. πŸ’€ bestie i cannot stress this enough β€” multithreaded + these hooks = chaos.

From glibc 2.24 onwards, the __malloc_initialize_hook variable has been removed from the API. and from glibc 2.34 onwards, all the hook variables have been removed from the API. 😭 they're gone gone. not "deprecated but lurking." removed.

Programmers should instead preempt calls to the relevant functions by defining and exporting malloc(), free(), realloc(), and calloc(). that's the move, that's the main character play. ✨


πŸ“ EXAMPLES πŸ“

Here is a short example of how to use these variables. look at her go:

#include <stdio.h>
#include <malloc.h>

/* Prototypes for our hooks */
static void my_init_hook(void);
static void *my_malloc_hook(size_t, const void *);

/* Variables to save original hooks */
static typeof(void *(size_t, const void *))  *old_malloc_hook;

/* Override initializing hook from the C library */
typeof(void (void))  *__malloc_initialize_hook = my_init_hook;

static void
my_init_hook(void)
{
    old_malloc_hook = __malloc_hook;
    __malloc_hook = my_malloc_hook;
}

static void *
my_malloc_hook(size_t size, const void *caller)
{
    void *result;

    /* Restore all old hooks */
    __malloc_hook = old_malloc_hook;

    /* Call recursively */
    result = malloc(size);

    /* Save underlying hooks */
    old_malloc_hook = __malloc_hook;

    /* printf() might call malloc(), so protect it too */
    printf("malloc(%zu) called from %p returns %p\n",
            size, caller, result);

    /* Restore our own hooks */
    __malloc_hook = my_malloc_hook;

    return result;
}

the way this swaps hooks out before calling malloc(size) and swaps them back after? 🧠 that's the recursion protection, that's the whole trick, we love a queen who protects herself.


πŸ‘€ SEE ALSO πŸ‘€

mallinfo(3), malloc(3), mcheck(3), mtrace(3) β€” go read those next bestie, they're the supporting cast and they ate πŸ’…


okay that's the whole page!! mwah πŸ’‹ this was Linux man-pages 6.20, dated 2026-02-08, and yes the girlies at __malloc_hook(3) said goodbye. deprecated but never forgotten. πŸŽ€βœ¨πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from
  • arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • πŸ“œ the page's license: GPL-1.0-or-later
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms