Trying to reduce size of memory allocations in debug compilations for C++

353 views Asked by At

I have a software that is compiled in debug mode for ease of support (getting dump files, etc.). Visual studio version is 2008.

We have noticed that our memory allocations suffer a huge overhead as a result. Every memory allocation (even of a single pointer) done when DEBUG is defined takes with it a big header identifying the allocating file, line, etc.

We are trying to stay in DEBUG and yet reduce the overhead. It seems that defining _AFX_NO_DEBUG_CRT should do the trick - but defining it on the project level or in our stdafx.h file leads to a long list of compilation errors:

c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afxtls_.h(66) : error C2059: syntax error : '__asm' c:\program files (x86)\microsoft visual studio 9.0\vc\atlmfc\include\afxtls_.h(66) : error C2143: syntax error : missing ')' before '{' ....

The error is the same is reported under : #define _AFX_NO_DEBUG_CRT causes a stream of compilation errors, but the requirement to create your own new is not relevant here and I found no solution there.

Another alternative I saw in posts is playing with _CrtSetDbgFlag - but that seems just to avoid writing the information to the heap - but still allocates the memory for it - that's not what we are looking for.

Any ideas?

1

There are 1 answers

4
user1 On
#define _CRTDBG_MAP_ALLOC

From MSDN, The #define statement maps the base versions of the CRT heap functions to the corresponding debug versions. By including crtdbg.h, you map the malloc and free functions to their debug versions, _malloc_dbg and _free_dbg, which keep track of memory allocation and deallocation. This is what you want to avoid. This mapping occurs only in a debug build (in which _DEBUG is defined). Release builds use the ordinary malloc and free functions.

To serve your purpose, just #undef _CRTDBG_MAP_ALLOC.

Go to Visual Studio project settings - > Debug settings, go to C/C++ Preprocessor settings, Add _CRTDBG_MAP_ALLOC macro under "Undefine Preprocessor Definitions" settings.

You should be able to generate a DEBUG executable which will not generate overheard during memory allocations at runtime.