Verbose Mode C++

Here is a quick tip for debugging a section of a C++ application. Often times, when you are in need of a quick way to debug a part of your code and do not want to go through the whole program and digest each line of code, you can use #directive. Below is an example of using this "verbose" directive:


#ifdef DEBUGMEM
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif

#include <iostream>              

using namespace std;            

void test()
{
       // Do something
}  

void main()                     
{
       test();

       #ifdef DEBUGMEM 
        _CrtDumpMemoryLeaks();  
       #endif   
}

When you put #define DEBUGMEM at the beginning of your code, the code inside the #ifdef and #endif will automatically be executed. This enables you to easily disable or enable each part of your code if needed. 

In this particular code, the _CrtDumpMemoryLeaks(); and its corresponding headers will only be included and executed when #define DEBUGMEM is present in your program.

Imagine what you could do with these tags!!!

Comments

Post a Comment

Popular posts from this blog

Creating Markdown Blog with ASP.NET and C# - Part 1

Markdown vs. the World