Function Arguments

Chapter 14 - Advanced Programming Topics

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Conditional Compilation
Preprocessor statements aren’t always found in header files. Preprocessor directives can be used in a program’s source code to generate efficient compilations. Look at this next code segment and see if you can detect the subtle difference (hint: executable code size):
/* compiled if statement */
if(DEBUG_ON) {
 printf(“Entering Example Function”);
 printf(“First argument passed has a value of %d”,ifirst_arg);
}

/* comparison statement  */
#if defined(DEBUG_ON)
 printf(“Entering Example Function”);
 printf(“First argument passed has a value of %d”,ifirst_arg);
#endif
The first if statement is always compiled. This means that the debugging information is perpetually reflected in the executable size of your program. But what if you don’t want to ship a product with your intermediate development cycle code? The solution is to conditionally compile these types of statements.
The second portion of the code demonstrates how to selectively compile code with the #if-defined directive. To debug your program, you simply define DEBUG_ON. This makes the nested #if...#endif statements visible to the compiler. However, when you are ready to ship the final product, you remove the DEBUG_ON definition. This makes the statements invisible to the compiler, reducing the size of the executable file.
Try the following simple test to prove to yourself how invisible the #if...#endif directives make the printf( ) statement pair. Copy the previous code segment into a simple C program that does nothing else. Include all necessary overhead (#include, main( ), {, and so on). Do not define DEBUG_ON. Make certain that when you compile the program, there are no error messages. Now, remove the #include <stdio.h> statement from the program and recompile.
At this point, the compiler stops at the first printf( ) statement nested within the if…printf( ) block statement. The message printed is “Function ‘printf‘ should have a prototype.” You would expect this since the printf( ) statement within the if is always visible to the compiler. Now, simply remove or comment out the if...printf( ) block statement and recompile.
The compiler does not complain about the printf( ) statements nested within the #if...#endif preprocessors. It never saw them. They would only become visible to the compilation phase of the compiler if DEBUG_ON is defined. You can use this selective visibility for more than executable statements. Look at this next code streamlining option:
#if defined(DEBUG_ON)
 /*****************************************/
 /* The following code segment performs   */
 /* a sophisticated enough solution step  */
 /* to require a comment and debug output */
 /*****************************************/
 printf(“    debug code goes here        ”);
#endif
This example not only has a conditional output debug statement, but it also provides room for an explanatory comment. The little extra time it takes to write conditionally compiled code has its trade-off in easily debugged code and small executable code size.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net