Example (_dump_allocated -- Get Information about Allocated Memory)

This example allocates three memory blocks, and then calls _dump_allocated to dump information and the first 8 bytes for each memory block.

Note: You must compile this example with the /Tm option to map the memory management functions to their debug versions.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define  INIT_STR      "It\0works"
#define  INIT_STR_LEN  (sizeof(INIT_STR) - 1)
int main(void) {
   char *pBlock1;
   char *pBlock2;
   char *pBlock3;
   if (NULL == (pBlock1 = (char*)malloc(35))) { /* allocate first memory block*/
      printf("Could not allocate first memory block.\n");
      return  EXIT_FAILURE;
   }
   memcpy(pBlock1, INIT_STR, INIT_STR_LEN); /* initialize first memory block  */
   if (NULL == (pBlock2 = (char*)calloc(2, 120))) { /* allocate second
                                                                memory block  */
      printf("Could not allocate second memory block.\n");
      return  EXIT_FAILURE;
   }
   memcpy(pBlock2, INIT_STR, INIT_STR_LEN); /* initialize second memory block */
   if (NULL == (pBlock3 = (char*)realloc(NULL, 2235))) {  /* allocate third
                                                             memory block     */
      printf("Could not allocate third memory block.\n");
      return  EXIT_FAILURE;
   }
   memcpy(pBlock3, INIT_STR, INIT_STR_LEN); /* initialize third memory block  */
   if (NULL == (pBlock3 = (char*)realloc(pBlock3, 300))) {/* reallocate third */
                                      /* memory block to different size       */
      printf("Could not reallocate third memory block.\n");
      return  EXIT_FAILURE;
   }
   _dump_allocated(8);    /* show first eight bytes of each memory block      */
   return 0;
}
   /****************************************************************************
      The output should be similar to:
      -------------------------------------------------------------------------------
                       START OF DUMP OF ALLOCATED MEMORY BLOCKS
      -------------------------------------------------------------------------------
      Address: 0x00074310      Size: 0x0000012C (300)
      This memory block was (re)allocated at line number 32 in _dump_alloc.c.
      Memory contents:  49740077 6F726B73                      [It.works        ]
      -------------------------------------------------------------------------------
      Address: 0x000738F0      Size: 0x000000F0 (240)
      This memory block was (re)allocated at line number 19 in _dump_alloc.c.
      Memory contents:  49740077 6F726B73                      [It.works        ]
      -------------------------------------------------------------------------------
      Address: 0x00073890      Size: 0x00000023 (35)
      This memory block was (re)allocated at line number 13 in _dump_alloc.c.
      Memory contents:  49740077 6F726B73                      [It.works        ]
      -------------------------------------------------------------------------------
                       END OF DUMP OF ALLOCATED MEMORY BLOCKS
      -------------------------------------------------------------------------------
   ****************************************************************************/
}