Debug Versions of Heap Allocation Functions
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, use the version selector at the top left. We recommend upgrading to Visual Studio 2019. Download it here
The C run-time library contains special Debug versions of the heap allocation functions. These functions have the same names as the Release versions with _dbg appended to them. This topic describes the differences between the Release version of a CRT function and the _dbg version, using
_malloc_dbg as examples.
You might want to call
_malloc_dbg explicitly, however. Calling
_malloc_dbg explicitly has some added benefits:
Storing the source file and line number where the allocation request occurred.
If you do not want to convert your
_malloc_dbg, you can obtain the source file information by defining _CRTDBG_MAP_ALLOC, which causes the preprocessor to directly map all calls to
_malloc_dbginstead of relying on a wrapper around
To track the separate types of allocations in client blocks, you must call
_malloc_dbgdirectly and set the
When _DEBUG is not defined, calls to
mallocare not disturbed, calls to
_malloc_dbgare resolved to
malloc, the definition of _CRTDBG_MAP_ALLOC is ignored, and source file information pertaining to the allocation request is not provided. Because
mallocdoes not have a block type parameter, requests for
_CLIENT_BLOCKtypes are treated as standard allocations.