TODO: How to just have one function?



  • I remember showing preprocessor macros to someone once, and that you
    can conditionally compile sections of code with #ifdef and friends.

    Looking over there code a recently, it seems they didn't quite understand me, or just lost the plot somewhere. 

    // Set to zero to use secondary algorithm, not zero to use primary.

    #define ZRQ 1

    void doAlgo0(void)

    {

      if (!ZRQ) // Why doesn't this work around the function signature?

      {

      }

    }

    void doAlgo1(void)
    {
      if (ZRQ) // Alternative to above. Only ever run one.
      {
      }
    }

    [...]

        doAlgo0(); doAlgo1(); // Only one is ever actually executed. See top of file. TODO: How to just have one function?

    [...]

     



  • @Mole said:

    // Set to zero to use secondary algorithm, not zero to use primary.

    #define ZRQ 1

    void doAlgo0(void)

    {

      if (!ZRQ) // Why doesn't this work around the function signature?

      {

      }

    }

    void doAlgo1(void)
    {
      if (ZRQ) // Alternative to above. Only ever run one.
      {
      }
    }

    [...]

        doAlgo0(); doAlgo1(); // Only one is ever actually executed. See top of file. TODO: How to just have one function?

    [...]

      This is great, because by doing it this way you can dynamically select which algorithm to use at runtime by simply changing the value of the #define!

     



  • @DaveK said:

    This is great, because by doing it this way you can dynamically select
    which algorithm to use at runtime by simply changing the value of the
    #define!

     

    You might want to think that through again.



  • @Zylon said:

    @DaveK said:

    This is great, because by doing it this way you can dynamically select
    which algorithm to use at runtime by simply changing the value of the
    #define!

     

    You might want to think that through again.

    #define ZRQ zrq
    zrq;
    
    ...
      zrq = !!getenv("USE_ALGO1")
    


  •  At the very least, why didn't he just make 1 function with if (ZRQ) { ... } else { ... } containing the different algos?



  • @morbiuswilters said:

     At the very least, why didn't he just make 1 function with if (ZRQ) { ... } else { ... } containing the different algos?


    That would be sufficiently equivalent to #if ZRQ ... #else ... #endif and not WTF-worthy, so it wouldn't have been posted.



  • @strcmp said:

    @morbiuswilters said:

     At the very least, why didn't he just make 1 function with if (ZRQ) { ... } else { ... } containing the different algos?


    That would be sufficiently equivalent to #if ZRQ ... #else ... #endif and not WTF-worthy, so it wouldn't have been posted.

    I think it would still be plenty WTFy.  Not comprehending macros is pretty dumb but writing two functions and having only one of them execute (but still understanding that "if (ZRQ)" around the body will enable/disable that logic) shows a much deeper problem.  Clearly he understands if statements on some level.  He probably understands else conditions as well.



  • @Zylon said:

    @DaveK said:

    This is great, because by doing it this way you can dynamically select
    which algorithm to use at runtime by simply changing the value of the
    #define!

     

    You might want to think that through again.

    Woooosh!



  • @DaveK said:

    @Zylon said:

    @DaveK said:

    This is great, because by doing it this way you can dynamically select
    which algorithm to use at runtime by simply changing the value of the
    #define!

     

    You might want to think that through again.

    Woooosh!
     

    Yeah... no. If that was a joke, try harder. Just posting something stupid will only accomplish people thinking you're stupid.



  • Maybe that was the sound of comprehension flying over his head?



  • @Zylon said:

    @DaveK said:

    @Zylon said:

    @DaveK said:

    This is great, because by doing it this way you can dynamically select
    which algorithm to use at runtime by simply changing the value of the
    #define!

     

    You might want to think that through again.

    Woooosh!
     

    Yeah... no. If that was a joke, try harder. Just posting something stupid will only accomplish people thinking you're stupid.

    Not all humour comes with big red blinking <JOKE> tags.  Some of it is delivered deadpan.  See also:


    p.s.:

    void switch_algo (int zrq)
    {
    char buffer[500];
    sprintf (buffer, "rm zrq.h ; echo '#define ZRQ %d' > zrq.h ; cc zrq.c -o %s", zrq, argv[0]);
    exec(argv[0]);
    }

    Use your imagination for goodness' sake! 



  • @DaveK said:

    void switch_algo (int zrq)
    {
    char buffer[500];
    sprintf (buffer, "rm zrq.h ; echo '#define ZRQ %d' > zrq.h ; cc zrq.c -o %s", zrq, argv[0]);
    exec(argv[0]);
    }

    Use your imagination for goodness' sake! 

    and whats that going to do, other than attempt to run itself? It's certainly not going to modify anything.



  • @Mole said:

    @DaveK said:
    void switch_algo (int zrq)
    {
    char buffer[500];
    sprintf (buffer, "rm zrq.h ; echo '#define ZRQ %d' > zrq.h ; cc zrq.c -o %s", zrq, argv[0]);
    exec(argv[0]);
    }
    Use your imagination for goodness' sake!
    and whats that going to do, other than attempt to run itself? It's certainly not going to modify anything.
    Assuming you're on a platform that lets you modify the files backing running executables, it'll re-run the program, but recompiled to use the algorithm passed in.



  • It looks to me that it writes a string to a buffer, then executes itself and finally destroys the string.  No where does it execute the contents of the string.

    Yes, I'm being pedantic, but this is the sidebar :-)



  • If you were being really pedantic, you'd notice <font face="courier new,courier">argv</font> isn't declared inside <font face="courier new,courier">switch_algo</font>.



  •  Sometimes people make argv global from within main() however, so I'd forgive that one. Expecting sprintf to execute the commands passed to however is less forgivable. 



  • Maybe he was thinking PHP's ` quotes and safe_mode off.



  • @derula said:

    Maybe he was thinking PHP's ` quotes and safe_mode off.
     

    Actually I was thinking this:

    @DaveK said:

    void switch_algo (int zrq)
    {
    char buffer[500];
    sprintf (buffer, "rm zrq.h ; echo '#define ZRQ %d' > zrq.h ; cc zrq.c -o %s", zrq, argv[0]);
    system (buffer);
    exec(argv[0]);
    }


    but I typo'd it.

     



  • @DaveK said:

    Actually I was thinking this:

    But the backtick operator is a so much more elegant solution! Should be part of C:

    #define ZERO `cat /dev/null`


  • @derula said:

    @DaveK said:
    Actually I was thinking this:

    But the backtick operator is a so much more elegant solution! Should be part of C:

    #define ZERO `cat /dev/null`

    C with shell syntax?  Take away the type system and what you've got sounds a lot like PERL!




  • @DaveK said:

    @derula said:

    @DaveK said:
    Actually I was thinking this:

    But the backtick operator is a so much more elegant solution! Should be part of C:

    #define ZERO `cat /dev/null`

    C with shell syntax?  Take away the type system everything but the punctuation and what you've got sounds a lot like PERL!

    FTFY

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.