Syntax
#include <malloc.h> int _heapchk(void);Description
A heap-specific version of this function, _uheapchk, is also available.
Note: Using the _heapchk, _heapset, and _heap_walk functions (and their heap-specific equivalents) may add overhead to each object allocated from the heap.
This example performs some memory allocations, then calls _heapchk to check the heap.
#include <stdlib.h>#include <stdio.h>
#include <malloc.h>
int main(void)
{
char *ptr;
int rc;
if (NULL == (ptr = malloc(10))) {
puts("Could not allocate memory block.");
exit(EXIT_FAILURE);
}
*(ptr - 1) = 'x'; /* overwrites storage that was not allocated */
if (_HEAPOK != (rc = _heapchk())) {
switch(rc) {
case _HEAPEMPTY:
puts("The heap has not been initialized.");
break;
case _HEAPBADNODE:
puts("A memory node is corrupted or the heap is damaged.");
break;
case _HEAPBADBEGIN:
puts("The heap specified is not valid.");
break;
}
exit(rc);
}
free(ptr);
return 0;
/****************************************************************************
The output should be similar to :
A memory node is corrupted or the heap is damaged.
****************************************************************************/
}
Related Information