Syntax
#include <malloc.h> int _heapset(unsigned int fill);Description
Using _heapset can help you locate problems where your program continues to use a freed pointer to an object. After you set the free heap to a specific value, when your program tries to interpret the set values in the freed object as data, unexpected results occur, indicating a problem.
A heap-specific version of this function, _uheapset, 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 allocates and frees memory, then uses _heapset to set the free heap to X. It checks the return code from _heapset to ensure the heap is still valid.
#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);
}
memset(ptr,'A',5);
free(ptr);
if (_HEAPOK != (rc = _heapset('X'))) {
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);
}
return 0;
}
Related Information