function using "define"

This is a discussion on function using "define" within the C++ Programming forums, part of the General Programming Boards category; So I am trying to make a program work and this is in one of the h files. Code: #define ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    7

    function using "define"

    So I am trying to make a program work and this is in one of the h files.

    Code:
    #define LogDebug( fmt, args... )    do { if ( gDebug )   { Log( fmt, ## args ); }} while (0)
    and it is giving me errors. can u declare a function like this or should i write an actual function in the cpp file?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The example you described is pointless to use macros with, use a function instead. If you're running the program in no-debug mode you'll get decreased efficiency since the if-statement is always checked. The way you coould define a more efficient debug macro is:
    Code:
    #ifdef DEBUG_MODE_ON
    #define LogDebug(fmt, args) Log(fmt, args);
    #else
    #define LogDebug(fmt, args)
    #endif
    This way if debug mode is off it will be expanded to nothing, no unneccessary code is run.
    Of course you cannot enable/disable debug mode at runtime, it has to be done at compile time...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    I take it your trying to forward a arbitrairy amount of arguements from LogDebug to Log. In that case:

    I believe there is a way to pass to have a variadic macro, but I do not know it.

    Regardless, you can rewrite Log() to take a va_list and pass that from LogDebug, writen as a regular function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Why don't you try

    Code:
    #if DEBUG
        int debug_fn(debug_info a, b, c)
        {
            //...
        }
        #define LOG_ERROR(a, b, c) debug_fn(a, b, c);
    #else
        #define LOG_ERROR(a, b, c)
    #endif
    It's a Very Bad Idea™ to use macros for complicated stuff like this. Make it a function.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    Using #if will make debug mode only controlled at compile time. This is generally perfectly fine for personal projects that will only be used on you computer. Runtime debugging is usefull for traking errors observed on another machine, such as if a user of your program reports a bug.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21