Debug Memory Management Functions

These functions provide information that you can use to debug memory problems. Use them to allocate and free memory from the default run-time heap, just as you would use the regular versions. In addition to their usual behavior, these functions store information (file name and line number) about each call made to them. Each call also automatically checks the heap by calling _heap_check.

When you use the /Tm compiler option, all calls to the regular memory management functions are mapped to their debug versions. You can also call the debug versions explicitly. If you parenthesize the calls to the regular memory management functions, they are not mapped to their debug versions.

If you have already compiled, and do not want to recompile, but still need to determine if any heap errors are present, you can relink your application with the /DEBUG linker option and cpphd.obj. This will give output similar to compiling with /Tm+, but will not include filenames or lines. Instead, tracebacks are given.

By default, strings are read-only (default setting is #pragma strings(readonly)). It is not essential that you use this directive, but it does ensure that the file name passed to the debug functions cannot be overwritten, and that only one copy of the file name string is included in the object module.

The names of the debug versions are prefixed by _debug_, for example, _debug_malloc, and they are defined in <malloc.h> and <stdlib.h>

The functions provided are:

_debug_calloc _debug_free _debug_heapmin _debug_malloc
_debug_realloc _debug_new _debug_memcpy _debug_memmove
_debug_memset _debug_strcat _debug_strcpy _debug_strdup
_debug_strncat _debug_strncpy _debug_strset _debug_strnset

Three additional debug memory management functions do not have regular counterparts:

_dump_allocated
Prints information to file handle 2 (the usual destination of stderr) about each memory block currently allocated by the debug functions. You can change the destination of the information with the _set_crt_msg_handle function.
_dump_allocated_delta
Prints information to file handle 2 about each memory block allocated by the debug functions since the last call to _dump_allocated or _dump_allocated_delta. Again, you can change the destination of the information with the _set_crt_msg_handle function.
_heap_check
Checks all memory blocks allocated or freed by the debug functions to make sure that no overwriting has occurred outside the bounds of allocated blocks or in a free memory block.
 
The debug functions call _heap_check automatically; you can also call it explicitly. To use _dump_allocated and _dump_allocated_delta, you must call them explicitly.
 
In releases prior to VisualAge for C++ Version 3.0, you could not mix debug and regular versions of the memory management functions. For example, you could not allocate memory with malloc and free it with _debug_free. This restriction no longer applies; realloc and free (debug or otherwise) can now handle memory allocated by any other allocation function.


Types of Memory
Memory Management Functions


Write Programs for Debugging