mcheck(3)
β¨ mcheck(3) β¨ β heap consistency checking, bestie edition π
okay so listen, you know how malloc(3) and its whole friend group can just likeβ¦ lie to you and corrupt the heap and you'd have NO idea until everything explodes? yeah. that's what these functions are for. this is mcheck(3) and it is lowkey the vibe check your memory has been begging for π«Ά
π LIBRARY
Standard C library (libc, -lc)
πΈ SYNOPSIS
#include <mcheck.h>
int mcheck(typeof(void (enum mcheck_status mstatus)) *f);
int mcheck_pedantic(
typeof(void (enum mcheck_status mstatus)) *f);
void mcheck_check_all(void);
enum mcheck_status mprobe(void *ptr);
π DESCRIPTION
so mcheck() installs a set of debugging hooks onto the malloc(3) family of memory-allocation functions β like hiring a security guard for your heap π§ and those hooks make certain consistency checks happen on the state of the heap. the checks can catch application errors, like freeing a block of memory more than once, or corrupting the bookkeeping data structures that sit immediately before a block of allocated memory. messy!!
β οΈ to actually be effective,mcheck()has to be called before the first call tomalloc(3)or any related function. no exceptions, no "oops I forgot", periodt.
if that's hard to guarantee (it usually is, let's be real π), linking the program with -lmcheck inserts an implicit call to mcheck() β with a NULL argument β before the first call to a memory-allocation function. automatic. iconic. π
mcheck_pedantic() is basically mcheck() but with main character energy: it performs checks on all allocated blocks whenever one of the memory-allocation functions is called. she does NOT skip leg day.
π heads up bestie: this can be very slow!
mcheck_check_all() causes an immediate check on all allocated blocks. but these instructions are clear β this call is effective only if mcheck() was called beforehand. iykyk.
if the system detects an inconsistency in the heap, the caller-supplied function pointed to by f is invoked with a single argument, mstatus, that tells you what type of inconsistency got detected. and if f is NULL? a default function prints an error message on stderr and calls abort(3). π that's a whole situation.
mprobe() performs a consistency check on the block of allocated memory pointed to by ptr. and yes, mcheck() should be called beforehand β otherwise mprobe() returns MCHECK_DISABLED. it's giving "shoulda planned ahead".
so here's the list of values returned by mprobe() or passed as the mstatus argument when f is invoked β memorize them, there's a quiz later (there is no quiz, but still):
MCHECK_DISABLED(mprobe()only) βmcheck()was not called before the first memory allocation function was called. consistency checking is not possible. tragic honestly.MCHECK_OK(mprobe()only) β no inconsistency detected. she's clean. β¨MCHECK_HEADβ memory preceding an allocated block was clobbered. π¬MCHECK_TAILβ memory following an allocated block was clobbered. π¬MCHECK_FREEβ a block of memory was freed twice. the classic!
π RETURN VALUE
mcheck() and mcheck_pedantic() return 0 on success, or -1 on error. that's it, that's the whole thing.
π₯ ATTRIBUTES
for an explanation of the terms used in this section, go see attributes(7). here's the tea π΅:
- Interface:
mcheck(),mcheck_pedantic(),mcheck_check_all(),mprobe()- Attribute: Thread safety
- Value:
MT-Unsafe race:mcheck const:malloc_hooks
β STANDARDS
GNU. (yes, just GNU π«‘)
π¦ HISTORY
mcheck_pedantic()mcheck_check_all()
β glibc 2.2.
mcheck()mprobe()
β glibc 2.0.
π NOTES
linking a program with -lmcheck and using the MALLOC_CHECK_ environment variable (described over in mallopt(3)) cause the same kinds of errors to be detected. the difference? using MALLOC_CHECK_ does not require the application to be relinked. convenient!! π
π EXAMPLES
the program below calls mcheck() with a NULL argument and then frees the same block of memory twice. and the following shell session demonstrates what happens when running the program β spoiler alert, it does not end well:
$ ./a.out
About to free
About to free a second time
block freed twice
Aborted (core dumped)
core dumped, bestie. we told her. π
π§ Program source
#include <mcheck.h>
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
char *p;
if (mcheck(NULL) != 0) {
fprintf(stderr, "mcheck() failed\n");
exit(EXIT_FAILURE);
}
p = malloc(1000);
fprintf(stderr, "About to free\n");
free(p);
fprintf(stderr, "\nAbout to free a second time\n");
free(p);
exit(EXIT_SUCCESS);
}
π«Ά SEE ALSO
malloc(3), mallopt(3), mtrace(3)
okay that's the whole page, no cap π this one was pulled straight out of Linux man-pages 6.20, dated 2026-02-08 β go forth and let your heap be honest for once πβ¨π
π¦ 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
- Copyright 2012, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms