Hello there, just a question, what do you think is the purpose of this flag to this block of code...

void CDriveControl::Trace(bool bAt, char *lpszFormat, ...)
#ifdef _DEBUG
    va_list args;
    va_start(args, lpszFormat);

    printf(lpszFormat, args);
    if( bAt) {
        fflush( stdout);

I've been wondering, why do you have to check if the _DEBUG flag is defined?

9 Years
Discussion Span
Last Post by jaepi

If the _DEBUG flag is not defined, the code inside the #ifdef #endif preprocessor directives will not be compiled and linked.

The code inside the directives makes the program slow and bulky. So allthough you want it for debugging, you will not want that part of code inside the release version of the executable. Erasing it all before releasing is tedious and you may leave some behind. By using the preprocessor directives, you can produce a bulky slow, debugging version by defining the _DEBUG flag, and produce a smaller faster release version by not defining the _DEBUG flag. You do not have to make any changes to the code, since the compiler can be directed to define the flags by the command line and usually it is done by the makefile.

For Visual C++ it is /D_DEBUG . For gcc, lookup the compiler manual under preprocessor directives. cl /D_DEBUG Trace.cpp will compile and link the Trace.cpp file with the _DEBUG flag defined. cl Trace.cpp will not define the _DEBUG flag.



This code is for Linux so it'll be in gcc compiler. Oh I see. Btw, how do these debug codes inside the directives work? I mean, why does it have to use the va_ family of functions? *sigh*


Because the Trace function is to be used as a general purpose function, the writer of the code doesnt know what type of arguments and how many arguments will be passed. In other words Trace is a variable argument function. You can know if a function is a varibale argument function by looking at the ... ellipsis at the argument list in the definition. The va* family of functions are used to extract the number of parameters passed during runtime, and extract them.

Also see if you can understand this.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.