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>_heapchk() checks heap for consistency</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 _heapchk()              Checks heap for consistency

 #include   <malloc.h>

  int _heapchk(void);

    _heapchk() does a minimal consistency check on the unallocated memory
    space, or "heap". The consistency check determines whether all the
    heap entries are within the bounds of the heap's current memory
    allocation.

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

    Data model   Program model    _heapchk() mapped to
    ----------   -------------    --------------------
      small         small         _nheapchk() - near heap
      small         medium        _nheapchk() - near heap
      large         compact       _fheapchk() - far heap
      large         large         _fheapchk() - 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     Bad node found, or heap damaged.

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

   Portability:     MS-DOS only

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

 This program checks heap for consistency after several calls to malloc()
 and free()

           #include<malloc.h>

           main()
           {
              void chk_heap();
              char *ptr1, *ptr2;

              chk_heap();
              ptr1 = malloc(100);
              chk_heap();
              malloc(500);
              chk_heap();
              ptr2 = malloc(100);
              chk_heap();
              free(ptr1);
              chk_heap();
              free(ptr2);
              chk_heap();
           }

           void chk_heap()
           {
              int heapstatus;

              heapstatus = _heapchk();
              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: _heapset() _heapwalk()

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