Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Runs consistency checks on the heap.

int _heapchk( void );

_heapchk returns one of the following integer manifest constants defined in Malloc.h.


Initial header information is bad or cannot be found.


Bad node has been found or heap is damaged.


Pointer into heap is not valid.


Heap has not been initialized.


Heap appears to be consistent.

In addition, if an error occurs, _heapchk sets errno to ENOSYS.

The _heapchk function helps debug heap-related problems by checking for minimal consistency of the heap. If the operating system does not support _heapchk (for example, Windows 98), the function returns _HEAPOK and sets errno to ENOSYS.

Routine Required header Optional header Compatibility




Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For more compatibility information, see Compatibility in the Introduction.

// crt_heapchk.c
// This program checks the heap for
// consistency and prints an appropriate message.
#include <malloc.h>
#include <stdio.h>

int main( void )
   int  heapstatus;
   char *buffer;

   // Allocate and deallocate some memory
   if( (buffer = (char *)malloc( 100 )) != NULL )
      free( buffer );

   // Check heap status
   heapstatus = _heapchk();
   switch( heapstatus )
   case _HEAPOK:
      printf(" OK - heap is fine\n" );
   case _HEAPEMPTY:
      printf(" OK - heap is empty\n" );
      printf( "ERROR - bad start of heap\n" );
      printf( "ERROR - bad node in heap\n" );


 OK - heap is fine

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

© 2015 Microsoft