Format
#include <malloc.h> int _heapchk(void);
Language Level: Extension
_heapchk checks the default storage heap for
minimal consistency by checking all allocated and freed objects
on the heap.
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.
Return Value
_heapchk returns one of the following
values, defined in <malloc.h>:
| _HEAPBADBEGIN | The heap specified is not valid. (Only occurs if you changed the default heap and then later closed or destroyed it.) |
| _HEAPBADNODE | A memory node is corrupted, or the heap is damaged. |
| _HEAPEMPTY | The heap has not been initialized. |
| _HEAPOK | The heap appears to be consistent. |
Example
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 = (char*)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. ****************************************************************/ }
![]()
_debug_heapmin -- Release
Unused Memory in the Default Heap
_heapmin --
Release Unused Memory from Default Heap
_heapset --
Validate and Set Default Heap
_heap_walk --
Return Information about Default Heap
<malloc.h>
<stdlib.h>