Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

More on the C/C++ Comment Macro for Debug Statements


More on the C/C++ Comment Macro for Debug Statements A C/C++ Comment Macro" by Mark Timperley in the January 2001 issue of C/C++ Users Journal suggested a macro definition to comment out a single-line debug statement. The tip created a macro to represent two slash characters (//) as the beginning of a single-line statement. For debug builds, the single line compiled. For release builds, the line was interpreted as a comment. Mr. Timperley elaborated a token-pasting operator in a second macro, SLASH(), in order to compose the two slash characters as shown:

 #define COMMENT SLASH(/)
 #define SLASH(s) /##s
 
 #if _DEBUG
 #include <iostream>
 #define dout std::cout
 #else
 #define dout COMMENT
 #endif
 
 dout << "Hello World!\n";
 
However, this approach is limited to only single-line debug comments, and its utility relies on the compiler implementation (i.e., the implementation of the C/C++ preprocessor). For instance, some preprocessor implementations do not immediately interpret the macro token-pasting operation used in another macro definition. These preprocessors will define the macro COMMENT as a "SLASH(/)" char string instead of "//" as desired. Hence, this approach may not be portable across different compilation environments. This technique also does not satisfy the scenario where the debug statements are formatted into multiple lines to improve legibility. For example, in the following code segment:

 dout << "variable a: " << a // a is an integer
 << "variable b: " << b /* b is a double */
 << "variable c: " << c // c is a char
 << endl;
 
Only the first line will be commented out, and the rest of the lines will cause compilation errors. (Note that I included the comment control characters /*, */, and // in the preceding code segment to increase the complexity.)

A recent assignment at FedEx Services ITD achieved a couple of portable C/C++ comment macro definitions that are able to handle multiple-line debug statements in both function (dbgInC) and class (dbgInCpp) formats. An example of these useful and portable macro tricks is illustrated below:

 #if _DEBUG
 // dbgInC defined as "printf" or other custom debug function
 #define dbgInC printf
 // dbgInCpp defined as "cout" or other custom debug class
 #define dbgInCpp cout
 #else
 // dbgInC defined as null [1]
 #define dbgInC
 // dbgInCpp defined as "if(0) cerr" or "if(1); else cerr"
 #define dbgInCpp if(0) cerr
 #endif
 
 dbgInC("Debug in C: %d%s%2.0f\n",
 a, // a is an integer
 b, /* b is char array */
 c); // c is a float
 
 dbgInCpp << "Debug in C++: "
 << a // a is an integer
 << b /* b is char array */
 << c // c is a float
 << endl;
 
When compiled, dbgInC is interpreted as null by the preprocessor, and dbgInCpp is interpreted as a "if(0) cerr" string.

The function dbgInC is converted to an expression that does not cause a compilation error but returns a true/false value at run time.

 ("Debug in C: %d%s%2.0f\n", a, b, c);
 
And dbgInCpp is converted to an if-condition whose statement, cerr << ..etc., will never be reached at run time.

 if(0)
 cerr << "Debug in C++: " << a << b
 << c << endl;
 
 or [2]:
 
 if(1);
 else
 cerr << "Debug in C++: " << a << b
 << c << endl;
 
The proposed comment macro definitions convert the debug statements into unrelated C/C++ code and are not limited by the implementation of the C/C++ preprocessor. Thus, they are highly portable across different compilers. But, will code conversion by macro definition affect the run-time performance significantly? I conducted a performance test of the above conversion macro approach against blank-test sample code where the debug statements were actually removed. Interestingly, the performance measurement showed the proposed conversion macro suffered merely a nonessential loss (less than 1~2%) versus the ideal blank test with Forte 6.2, the Sun Workstation Pro C++ compiler.

Notes

[1] Or, similar to dbgInCpp:

 #define dbgInC if(0) printf
 
[2] With appropriate compiler optimization level (e.g. -xO4 in Forte 6.2, Sun Workstation Pro C++ compiler), the if(0) condition and its statements can be stripped from the assembly code of compilation by the C++ preprocessor. Thus, the debug segment is actually removed from the code at release builds.

About the Author

Randall C. Chang graduated with a Ph.D. in electrical and computer engineering from University of Florida. Since 1994, he has provided computer software consulting services in the central Florida area to corporations such as Florida Power Corporation, Lucent Technologies, Inc., etc. He currently works as a technical advisor for FedEx Services Information Technology Division and teaches C++/Java programming language courses in the Management Information Systems Department at University of Central Florida.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.