Example (_udump_allocated_delta -- Get Information about Allocated Memory in Heap)

This example creates a heap and allocates memory from it. It then calls _udump_allocated to display information about the allocated memory. After performing more memory operations, it calls _udump_allocated_delta to display information about the memory allocated since the call to _udump_allocated.

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

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <umalloc.h>
int main(void)
{
   Heap_t  myheap;
   char    *ptr1, *ptr2;
   /* Use default heap as user heap */
   myheap = _udefault(NULL);
   if (NULL == (ptr1 = (char*)_umalloc(myheap, 10))) {
      puts("Cannot allocate first memory block from user heap.");
      exit(EXIT_FAILURE);
   }
   memset(ptr1, 'a', 5);
   _udump_allocated(myheap, 10);
   if (NULL == (ptr2 = (char*)_umalloc(myheap, 20))) {
      puts("Cannot allocate second memory block from user heap.");
      exit(EXIT_FAILURE);
   }
   memset(ptr2, 'b', 5);
   printf("\nResults of _udump_allocated_delta are:\n");
   _udump_allocated_delta(myheap, 10);
   free(ptr1);
   free(ptr2);
   return 0;
   /****************************************************************************
      The output should be similar to :
      -------------------------------------------------------------------------------
                       START OF DUMP OF ALLOCATED MEMORY BLOCKS
      -------------------------------------------------------------------------------
      Address: 0x00073890      Size: 0x0000000A (10)
      This memory block was (re)allocated at line number 14 in _udump_alloc_d.c.
      Memory contents:  61616161 61AAAAAA AAAA                 [aaaaaªªªªª      ]
      -------------------------------------------------------------------------------
                       END OF DUMP OF ALLOCATED MEMORY BLOCKS
      -------------------------------------------------------------------------------
      Results of _udump_allocated_delta are:
      -------------------------------------------------------------------------------
                       START OF DUMP OF ALLOCATED MEMORY BLOCKS
      -------------------------------------------------------------------------------
      Address: 0x000738D0      Size: 0x00000014 (20)
      This memory block was (re)allocated at line number 21 in _udump_alloc_d.c.
      Memory contents:  62626262 62AAAAAA AAAA                 [bbbbbªªªªª      ]
      -------------------------------------------------------------------------------
                       END OF DUMP OF ALLOCATED MEMORY BLOCKS
      -------------------------------------------------------------------------------
   ****************************************************************************/
}