_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 , _dump_allocated, or _dump_allocated_delta functions.

To use _debug_malloc, you must compile with the debug memory -qheapdebug compiler option. This option maps all malloc calls to _debug_malloc.

Note: The -qheapdebug option maps all calls to memory management functions (including heap-specific versions) 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 of this function (_debug_umalloc) is 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 -qheapdebug 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.
   ****************************************************************************/
}


Debugging Memory Heaps
Memory Management Functions
Managing Memory with Multiple Memory Heaps


Debugging Problems with Heap Memory


-qheapdebug Compiler Option
_debug_calloc - Allocate and Initialize Memory
_debug_free - Free Allocated Memory
_debug_heapmin - Free Unused Memory in the Default Heap
_debug_memcpy - Copy Bytes
_debug_memmove - Copy Bytes
_debug_memset - Set Bytes to Value
_debug_realloc - Reallocate Memory Block
_debug_strcat - Concatenate Strings
_debug_strcpy - Copy Strings
_debug_strncat - Concatenate Strings
_debug_strncpy - Copy Strings
_debug_strnset - Set Characters in String
_debug_strset - Set Characters in String
_debug_ucalloc - Reserve and Initialize Memory from User Heap
_debug_uheapmin - Free Unused Memory in User Heap
_debug_umalloc - Reserve Memory Block from User Heap