_debug_malloc -- Allocate Memory

Format

#include <stdlib.h>  /* also in <malloc.h> */
void *_debug_malloc(size_t size,
                    const char *file, size_t line);

Language Level: Extension
_debug_malloc is the debug version of malloc. Like malloc, it reserves a block of storage of size bytes from the default heap. _debug_malloc also sets all the memory it allocates to 0xAA, so you can easily locate instances where your program uses the data in the memory without initializing it first.

In addition, _debug_malloc makes an implicit call to _heap_check, and stores the file name file and the line number line where the storage is allocated. This information can later be used by the _heap_check and _dump_allocated or _dump_allocated_delta functions.

To use _debug_malloc, you must compile with the debug memory (/Tm) compiler option. This option maps all malloc calls to _debug_malloc.

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

To reallocate or free memory allocated by _debug_malloc, use _debug_realloc and _debug_free; you can also use realloc and free if you do not want debug information about the operation.

A heap-specific version (and a tiled version for OS/2) of this function (_debug_umalloc and _debug_tmalloc) are also available. _debug_malloc always allocates memory from the default heap.

Return Value
_debug_malloc returns a pointer to the reserved space. If not enough memory is available or if size is 0, _debug_malloc returns NULL.

Example
This example allocates 100 bytes of storage. It then attempts to write to storage that was not allocated. When _debug_malloc is called again, _heap_check detects the error, generates several messages, and stops the program.

Note: You must compile this example with the /Tm option to map the malloc calls to _debug_malloc.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
   char *ptr1, *ptr2;
   if (NULL == (ptr1 = (char*)malloc(100))) {
      puts("Could not allocate memory block.");
      exit(EXIT_FAILURE);
   }
   *(ptr1 - 1) = 'a';        /* overwrites storage that was not allocated    */
   ptr2 = (char*)malloc(10); /* this call to malloc invokes _heap_check      */
   puts("_debug_malloc did not detect that a memory block was overwritten.");
   return 0;
   /****************************************************************************
      Possible output is:
      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 _debug_mallo.c.
      Heap state was valid at line 8 of _debug_mallo.c.
      Memory error detected at line 13 of _debug_mallo.c.
   ****************************************************************************/
}


Managing Memory with Multiple Heaps
Memory Management


_debug_calloc -- Allocate and Initialize Memory
_debug_free -- Release Memory
_debug_realloc -- Reallocate Memory Block
_debug_umalloc -- Reserve Memory Blocks from User Heap
_dump_allocated -- Get Information about Allocated Memory
_dump_allocated_delta -- Get Information about Allocated Memory
_heap_check -- Validate Default Memory Heap
malloc -- Reserve Storage Block
<malloc.h>
<stdlib.h>
/Tm compiler option