Share via


Using the Debug Heap from C++

The debug versions of the C run-time library contain debug versions of the C++ new and delete operators. Unless you intend to make special use of the _CLIENT_BLOCK allocation type, be sure to define _CRTDBG_MAP_ALLOC when you are using C++. If this symbol is defined, all instances of new in your code are mapped properly to the debug version of new so as to record source file and line number information. If you use the _CLIENT_BLOCK type, you must call the debug version of the new operator directly or create macros that replace the new operator in debug mode, as shown in the following example:

/* MyDbgNew.h
/* Defines global operator new to allocate from
/* client blocks
*/
#ifdef _DEBUG
   #define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
   #define DEBUG_CLIENTBLOCK
#endif // _DEBUG


/* MyApp.cpp
/*  Compile options needed: /Zi /D_DEBUG /MLd
/*            or use a
/*      Default Workspace for a Console Application to
/*      build a Debug version
*/

#include "crtdbg.h"
#include "mydbgnew.h"

#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

void main( )  {
  char *p1;
  p1 =  new char[40];
  _CrtMemDumpAllObjectsSince( NULL );
 }

The debug version of the delete operator works with all block types and requires no changes in your program.