### _heapset -- Validate and Set Default Heap

Format

>     #include <malloc.h>
>     int _heapset(unsigned int fill);

Language Level: Extension
_heapset checks the default storage heap for minimal consistency by checking all allocated and freed objects on the heap (similar to _heapchk). It then sets each byte of the heap's free objects to the value of fill.

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.

Return Value
_heapset returns one of the following values, defined in <malloc.h>:

_HEAPBADBEGIN | The heap specified is not valid; it may have been closed or destroyed.
_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 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.

The allocated area is set to a string of 'A's (as the first line of the output shows). The area is freed, and then _heapset is called. All the freed areas are filled with 'X's (as the second line of the output shows). D, shown in the second line of the output, is part of the control info that the storage manager keeps in the first few bytes of every free area.

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

    int main (void)
    {
       char  *ptr, *ptr2;
       int   rc;

       if (NULL == (ptr = (char*)malloc(10))) {
          puts("Could not allocate memory block.");
          exit(EXIT_FAILURE);
       }
       if (NULL == (ptr2 = (char*)malloc(10))) {
          puts("Could not allocate memory block.");
          exit(EXIT_FAILURE);
       }
       memset(ptr,'A',10);
       printf("Contents of block: %.10s\n",ptr);
       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);
       }
       printf("Contents after block is freed and _heapset is done: %.10s\n",ptr);
       return 0;

       /****************************************************************************
          The output should be similar to:

          Contents of block: AAAAAA
          Contents after block is freed and _heapset is done: DXXXXX
       ****************************************************************************/
    }

Memory Management

_heapchk -- Validate Default Memory Heap
_heapmin -- Release Unused Memory from Default Heap
_heap_walk -- Return Information about Default Heap
_uheapset -- Validate and Set Memory Heap
<malloc.h>
<umalloc.h>