---
name: "mprobe"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/mcheck/3"
---

# mprobe(3)

heap consistency checking

Part of [mcheck(3)](/mcheck/3.md).

## NAME

mcheck, mcheck\_check\_all, mcheck\_pedantic, mprobe - heap consistency checking

## 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

The **mcheck**() function installs a set of debugging hooks for the [**malloc**(3)](/malloc/3.md) family of memory-allocation functions. These hooks cause certain consistency checks to be performed on the state of the heap. The checks can detect application errors such as freeing a block of memory more than once or corrupting the bookkeeping data structures that immediately precede a block of allocated memory.

To be effective, the **mcheck**() function must be called before the first call to [**malloc**(3)](/malloc/3.md) or a related function. In cases where this is difficult to ensure, linking the program with *-lmcheck* inserts an implicit call to **mcheck**() (with a NULL argument) before the first call to a memory-allocation function.

The **mcheck\_pedantic**() function is similar to **mcheck**(), but performs checks on all allocated blocks whenever one of the memory-allocation functions is called. This can be very slow!

The **mcheck\_check\_all**() function causes an immediate check on all allocated blocks. This call is effective only if **mcheck**() is called beforehand.

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 indicates what type of inconsistency was detected. If *f* is NULL, a default function prints an error message on *stderr* and calls [**abort**(3)](/abort/3.md).

The **mprobe**() function performs a consistency check on the block of allocated memory pointed to by *ptr*. The **mcheck**() function should be called beforehand (otherwise **mprobe**() returns **MCHECK\_DISABLED**).

The following list describes the values returned by **mprobe**() or passed as the *mstatus* argument when *f* is invoked:

- **MCHECK\_DISABLED** (**mprobe**() only)

  **mcheck**() was not called before the first memory allocation function was called. Consistency checking is not possible.

- **MCHECK\_OK** (**mprobe**() only)

  No inconsistency detected.

- **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.

## RETURN VALUE

**mcheck**() and **mcheck\_pedantic**() return 0 on success, or -1 on error.

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **mcheck**(), **mcheck\_pedantic**(), **mcheck\_check\_all**(), **mprobe**() | Thread safety | MT-Unsafe race:mcheck const:malloc\_hooks |

## STANDARDS

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 in [**mallopt**(3)](/mallopt/3.md)) cause the same kinds of errors to be detected. But, using **MALLOC\_CHECK\_** does not require the application to be relinked.

## EXAMPLES

The program below calls **mcheck**() with a NULL argument and then frees the same block of memory twice. The following shell session demonstrates what happens when running the program:

```
$ ./a.out
About to free
About to free a second time
block freed twice
Aborted (core dumped)
```

### 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)](/malloc/3.md), [**mallopt**(3)](/mallopt/3.md), [**mtrace**(3)](/mtrace/3.md)
