Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>_heapset() fills empty heap nodes</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 _heapset()              Fills empty heap nodes

 #include   <malloc.h>

  int _heapset(fill);
  unsigned int fill;     Fill character

    _heapset() first does a minimal consistency check on the unallocated
    memory space, or "heap" (just like _heapchk() does). Then it sets the
    heap's free entries with the fill value. This can be used in
    debugging to find where free nodes are located in memory dumps of the
    heap, and also to show where data was mistakenly written to memory
    that was freed.

    _heapset() will map to the appropriate version depending on the
    memory model used:

    Data model   Program model    _heapset() mapped to
    ----------   -------------    --------------------
      small         small         _nheapset() - near heap
      small         medium        _nheapset() - near heap
      large         compact       _fheapset() - far heap
      large         large         _fheapset() - far heap

    Returns:    One of the following manifest constants, defined in
                malloc.h:

                    Constant         Meaning
                    -------------    -----------------------------
                    _HEAPOK          The heap appears to be consistent.
                    _HEAPEMPTY       The heap has not been initialized.
                    _HEAPBADBEGIN    No initial header information found.
                    _HEAPBADNODE     A bad node found, or heap is
                                     damaged.

      Notes:    _heapset() is provided as an aid to debugging heap-
                related problems in programs.

   Portability:     MS-DOS only

 -------------------------------- Example ---------------------------------

 This program checks heap for consistency and fills empty nodes with 'X'.

           #include <malloc.h>

           main()
           {
              void set_heap();
              char *ptr1;

              set_heap('X');
              ptr1 = malloc(100);
              set_heap('X');
              malloc(500);
              set_heap('X');
              free(ptr1);
              set_heap('X');
           }

           void set_heap(unsigned int fill)
           {
              int heapstatus;

              heapstatus = _heapset(fill);
              switch (heapstatus)
                {
                case _HEAPOK:
                      printf("Heap is OK\n");
                      break;
                case _HEAPEMPTY:
                      printf("Heap is empty\n");
                      break;
                case _HEAPBADBEGIN:
                      printf("Heap has bad initial header\n");
                      break;
                case _HEAPBADNODE:
                      printf("Heap has bad node\n");
                      break;
                }

           }


See Also: _heapchk() _heapwalk()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson