Debug Versions of Heap Allocation Functions
Note
This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. 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
and _malloc_dbg
as examples.
When _DEBUG is defined, the CRT maps all malloc calls to _malloc_dbg. Therefore, you do not need to rewrite your code using _malloc_dbg
instead of malloc
to receive the benefits while debugging.
You might want to call _malloc_dbg
explicitly, however. Calling _malloc_dbg
explicitly has some added benefits:
Tracking
_CLIENT_BLOCK
type allocations.Storing the source file and line number where the allocation request occurred.
If you do not want to convert your
malloc
calls to_malloc_dbg
, you can obtain the source file information by defining _CRTDBG_MAP_ALLOC, which causes the preprocessor to directly map all calls tomalloc
to_malloc_dbg
instead of relying on a wrapper aroundmalloc
.To track the separate types of allocations in client blocks, you must call
_malloc_dbg
directly and set theblockType
parameter to_CLIENT_BLOCK
.When _DEBUG is not defined, calls to
malloc
are not disturbed, calls to_malloc_dbg
are resolved tomalloc
, the definition of _CRTDBG_MAP_ALLOC is ignored, and source file information pertaining to the allocation request is not provided. Becausemalloc
does not have a block type parameter, requests for_CLIENT_BLOCK
types are treated as standard allocations.