Share via


Using the Debug Version Versus the Base Version

The run-time library now contains special debug versions of the heap allocation functions that use the same names as the base versions and add the _dbg ending. This topic describes the differences in behavior between the debug version and the base version in a debug build of an application. The information in this topic is presented using malloc and _malloc_dbg as the example, but is applicable to all of the heap allocation functions.

Applications that contain existing calls to malloc do not need to convert their calls to _malloc_dbg to obtain the debugging features. When _DEBUG is defined, all calls to malloc are resolved to _malloc_dbg. However, explicitly calling _malloc_dbg allows the application to perform additional debugging tasks: it can separately track _CLIENT_BLOCK type allocations, and it can include the source file and line number where the allocation request occurred in the bookkeeping information stored in the debug header.

Because the base versions of the allocation functions are implemented as wrappers, the source file name and line number of each heap allocation request are not available by explicitly calling the base version. Applications that do not want to convert their malloc calls to _malloc_dbg can obtain the source file information by defining _CRTDBG_MAP_ALLOC. Defining _CRTDBG_MAP_ALLOC causes the preprocessor to directly map all calls to malloc to _malloc_dbg, thereby providing the additional information. To track particular types of allocations separately in client blocks, _malloc_dbg must be called directly and the blockType parameter must be set to _CLIENT_BLOCK.

When _DEBUG is not defined, calls to malloc are not disturbed, calls to _malloc_dbg are resolved to malloc, the definition of _CRTDBG_MAP_ALLOC is ignored, and source file information pertaining to the allocation request is not provided. Because malloc does not have a block type parameter, requests for _CLIENT_BLOCK types are treated as standard allocations.