Heap State Reporting Functions


Several new functions report the contents of the debug heap at a given moment. To capture a summary snapshot of the state of the heap at a given time, use the _CrtMemState structure defined in CRTDBG.H:

typedef struct _CrtMemState
{
// Pointer to the most recently allocated block:
   struct _CrtMemBlockHeader * pBlockHeader;  
// A counter for each of the 5 types of block:
   long lCounts[_MAX_BLOCKS];  
// Total bytes allocated in each block type:
   long lSizes[_MAX_BLOCKS];   
// The most bytes allocated at a time up to now:
   long lHighWaterCount;       
// The total bytes allocated at present:
   long lTotalCount;           
} _CrtMemState;

This structure saves a pointer to the first (most recently allocated) block in the debug heap’s linked list. Then, in two arrays, it records how many of each type of memory block (_NORMAL_BLOCK, _CLIENT_BLOCK, _FREE_BLOCK, and so forth) there are in the list, and the number of bytes allocated in each type of block. Finally, it records the highest number of bytes allocated in the heap as a whole up to that point, and the number of bytes currently allocated.

Table 1 shows the heap state reporting functions, which report the state and contents of the heap and use the information to help detect memory leaks and other problems.