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


Checks heaps for minimal consistency and sets the free entries to a specified value.

int _heapset( 
   unsigned int fill 


Fill character.

Return Value

_heapset returns one of the following integer manifest constants defined in MALLOC.H:

Initial header information invalid or not found.
Heap damaged or bad node found.
Heap not initialized.
Heap appears to be consistent.

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


The _heapset function shows free memory locations or nodes that have been unintentionally overwritten.

_heapset checks for minimal consistency on the heap, then sets each byte of the heap's free entries to the fill value. This known value shows which memory locations of the heap contain free nodes and which contain data that were unintentionally written to freed memory.

Note   In Visual C++ version 4.0, the underlying heap structure was moved to the C run-time libraries to support the new debugging features. As a result, the only Win32 platform that is supported by _heapset is Windows NT. The function returns _HEAPOK and sets errno to ENOSYS, when it is called by any other Win32 platform.


Routine Required header Optional headers Compatibility
_heapset <malloc.h> <errno.h> Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_heapset.c
/* This program checks the heap and
 * fills in free entries with the character 'Z'.

#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>

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

   if( (buffer = malloc( 1 )) == NULL ) /* Make sure heap is */
      exit( 0 );                        /*    initialized    */  
   heapstatus = _heapset( 'Z' );        /* Fill in free entries */
   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" );
   free( buffer );


OK - heap is fine

See Also

Memory Allocation Routines | _heapadd | _heapchk | _heapmin | _heapwalk | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft