malloc_get_state(3)

✨ malloc_get_state β€” bestie edition ✨

okay so real talk, this is malloc_get_state(3) from section 3 of the manual (Library Functions Manual, iykyk) β€” and yes she's a removed girlie, so buckle up πŸŽ€

πŸ’– NAME

malloc_get_state, malloc_set_state β€” record and restore state of malloc implementation

πŸ“š LIBRARY

Standard C library (libc, -lc)

🫢 SYNOPSIS

#include <malloc.h>

void *malloc_get_state(void);
int malloc_set_state(void *state);

🌸 DESCRIPTION

Note: these functions are removed in glibc 2.25. πŸ’€ like, gone gone. not "deprecated," removed.

So. The malloc_get_state() function records the current state of all malloc(3) internal bookkeeping variables β€” but NOT the actual contents of the heap, and NOT the state of malloc_hook(3) function pointers, okay, don't get it twisted πŸ™ˆ.

The state gets recorded into a system-dependent opaque data structure that's dynamically allocated via malloc(3), and a pointer to that data structure is what you get back as the function result. ✨ And bestie, heads up: it is the caller's responsibility to free(3) this memory. she does not clean up after you πŸ’…

The malloc_set_state() function restores the state of all malloc(3) internal bookkeeping variables to the values recorded in the opaque data structure pointed to by state.

πŸ”₯ RETURN VALUE

  • On success, malloc_get_state() returns a pointer to a newly allocated opaque data structure. On error (for example, memory could not be allocated for the data structure), malloc_get_state() returns NULL.
  • On success, malloc_set_state() returns 0.
  • If the implementation detects that state does not point to a correctly formed data structure, malloc_set_state() returns -1.
  • If the implementation detects that the version of the data structure referred to by state is a more recent version than this implementation knows about, malloc_set_state() returns -2. (she said "that's a future version, not my business" 😭)

πŸ‘‘ ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7).

  • Interface: malloc_get_state(), malloc_set_state() β€” Attribute: Thread safety β€” Value: MT-Safe πŸ¦‹

🌈 STANDARDS

GNU.

🧚 NOTES

These functions are useful when you're using this malloc(3) implementation as part of a shared library, and the heap contents are saved/restored via some other method. This technique is used by GNU Emacs to implement its "dumping" function. (main character behavior fr fr πŸ‘‘)

Hook function pointers are never saved or restored by these functions, with exactly two exceptions:

  • if malloc checking (see mallopt(3)) was in use when malloc_get_state() was called, then malloc_set_state() resets malloc checking hooks if possible;
  • if malloc checking was not in use in the recorded state, but the caller has requested malloc checking, then the hooks are reset to 0.

πŸ’œ SEE ALSO


that's the whole page bestie, no cap 🌸 this was Linux man-pages 6.20, dated 2026-02-08 β€” mwah, slay, periodt πŸ’…βœ¨πŸŽ€

πŸ“¦ 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: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms