### _heap_check -- Validate Default Memory Heap

Format

>     #include <stdlib.h>    /* also in <malloc.h> */
>     void _heap_check(void);

Language Level: Extension
_heap_check checks all memory blocks in the default heap that have been allocated or freed using the debug memory management functions (_debug_calloc, _debug_malloc, and so on). _heap_check checks that your program has not overwritten freed memory blocks or memory outside the bounds of allocated blocks.

When you call a debug memory management function (such as _debug_malloc), it calls _heap_check automatically. You can also call _heap_check explicitly. Place calls to _heap_check throughout your code, especially in areas that you suspect have memory problems.

Calling _heap_check frequently (explicitly or through the debug memory functions) can increase your program's memory requirements and decrease its execution speed. To reduce the overhead of heap-checking, you can use the CPP_HEAP_SKIP environment variable to control how often the functions check the heap. For example:

> `SET CPP_HEAP_SKIP =10`

specifies that every tenth call to any debug memory function (regardless of the type of heap) checks the heap. Explicit calls to _heap_check are always performed.

To use _heap_check and the debug memory management functions, you must compile with the debug memory (/Tm) compiler option.

Note: The /Tm option maps all calls to memory management functions (including heap-specific versions and tiled versions for OS/2) to their debug counterparts. To prevent a call from being mapped, parenthesize the function name.

_heap_check always checks the default heap.

Return Value
There is no return value.

Example
This example allocates 5000 bytes of storage, and then attempts to write to storage that was not allocated. The call to _heap_check detects the error, generates several messages, and stops the program. You must compile this example with the /Tm compiler option.

As the output shows, a string of AAAAAAs is put into a memory block when it is allocated.

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

    int main(void)
    {
       char *ptr;

       if (NULL == (ptr = (char*)malloc(5000))) {                                  /* line 8 */
          puts("Could not allocate memory block.");
          return EXIT_FAILURE;
       }

       *(ptr-1) = 'a';     /* overwrites storage that was not allocated */
       _heap_check();                                                              /* line14 */
       puts("_heap_check did not detect that a memory block was overwritten.");
       return 0;

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

          Header information of object 0x00073890 was overwritten at 0x0007388c.
          The first eight bytes of the memory block (in hex) are: AAAAAAAAAAAAAAAA.
          This memory block was (re)allocated at line number 8 in _heap_check.c.
          Heap state was valid at line 8 of _heap_check.c.
          Memory error detected at line 14 of _heap_check.c.
       ****************************************************************************/
    }

Managing Memory with Multiple Heaps
Memory Management

_debug_calloc -- Allocate and Initialize Memory
_debug_free -- Release Memory
_debug_heapmin -- Release Unused Memory in the Default Heap
_debug_malloc -- Allocate Memory
_debug_realloc -- Reallocate Memory Block
_dump_allocated -- Get Information about Allocated Memory
_dump_allocated_delta -- Get Information about Allocated Memory
_heapchk -- Validate Default Memory Heap
_uheap_check -- Validate User Memory Heap
<malloc.h>
<stdlib.h>
/Tm compiler option