Check heaps for minimal consistency and set the free entries to a specified value.
#include <malloc.h>
int _heapset( unsigned int fill );
int _bheapset( __segment seg, unsigned int fill );
int _fheapset( unsigned int fill );
int _nheapset( unsigned int fill );
fill | Fill character | |
seg | Specified based-heap segment selector |
The _heapset family of routines helps debug heap-related problems in programs by showing free memory locations or nodes unintentionally overwritten.
The _heapset routines first check for minimal consistency on the heap in a manner identical to that of the _heapchk functions. In addition, the _heapset functions set 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 locations contain data that were unintentionally written to freed memory.
The various _heapset functions check and fill these heaps:
Function | Heap Filled |
_heapset | Depends on data model of program. |
_bheapset | Based heap specified by seg value; _NULLSEG specifies all based heaps. |
_fheapset | Far heap (outside default data segment). |
_nheapset | Near heap (inside default data segment). |
In large data models (that is, compact-, large-, and huge-model programs), _heapset maps to _fheapset. In small data models (tiny-, small-, and medium-model programs), _heapset maps to _nheapset.
For _heapset, if the seg value is _NULLSEG, all based heap segments are checked; otherwise, only the specified one is checked.
All four routines return an int whose value is one of the following manifest constants (defined in MALLOC.H):
Constant | Meaning |
_HEAPBADBEGIN | Initial header information cannot be found, or it is invalid. |
_HEAPBADNODE | Bad node has been found, or the heap is damaged. |
_HEAPEMPTY | Heap has not been initialized. |
_HEAPOK | Heap appears to be consistent. |
_heapset
Standards:None
16-Bit:DOS
32-Bit:DOS32X
_bheapset, _fheapset
Standards:None
16-Bit:DOS, QWIN, WIN, WIN DLL
32-Bit:None
_nheapset
Standards:None
16-Bit:DOS
32-Bit:None
_heapchk functions, _heapwalk functions
/* 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>
void main( void )
{
int heapstatus;
char *buffer;
if( (buffer = malloc( 1 )) == NULL ) /* Make sure heap is initialized */
exit( 0 );
heapstatus = _heapset( 'Z' ); /* Fill in free entries */
switch( heapstatus )
{
case _HEAPOK:
printf( "OK - heap is fine\n" );
break;
case _HEAPEMPTY:
printf( "OK - heap is empty\n" );
break;
case _HEAPBADBEGIN:
printf( "ERROR - bad start of heap\n" );
break;
case _HEAPBADNODE:
printf( "ERROR - bad node in heap\n" );
break;
}
free( buffer );
}
OK - heap is fine