Preprocessor mastery



  • From the third-party source code of a device driver that I am maintaining:

     

     if((gcAtlSlots+gcIntlSlots)!= MAX_ATL_NODE) //Atl+Intl slots = MaxSlots. If user defines wrongly then the error validation is here
     {
      #undef gcIntlSlots
      #define gcIntlSlots (MAX_ATL_NODE -gcAtlSlots)
     }

    WTF? Real useful. Fortunately this seems to be an isolated incident of dimwittedness (so far).

     



  • looks like a workaround to me 



  • Why doesn't anymore people define their variables?

    int max(int a, int b)
    {
        if (a > b) {
           #define result a
        } else {
           #define result b
        }
        return result;



  • @ammoQ said:

    looks like a workaround to me 

    Please enlighten us.


  • @Zecc said:

    @ammoQ said:

    looks like a workaround to me 

    Please enlighten us.

     

    Workaround to correct the value of gcIntlSlots... Though it could be unconditional anyway.



  • somebody is apparently not getting the whole PRE-processor thingy.



  • Of course, people who actually know how the preprocessor works can be evil too: (this is from memory, but i've seen it many many times)

     #define ITERATE(_type, _array, _size, _code) \
        for (unsigned int currentIndex=0;currentIndex<(_size);currentIndex++) \
        { \
            _type currentValue = _array[currentIndex]; \
            _code \
        }

    [...]

        int array[512]; 
        ITERATE(int, array, 512,
             printf("[%d] = %d\n", currentIndex, currentValue); /* Imagine this code spanning 50 lines or so */
        )



  • D'oh... Thanks for the wake-up...



  • @bitpirate said:

    Of course, people who actually know how the preprocessor works can be evil too: (this is from memory, but i've seen it many many times)

     #define ITERATE(_type, _array, _size, _code) \
        for (unsigned int currentIndex=0;currentIndex<(_size);currentIndex++) \
        { \
            _type currentValue = _array[currentIndex]; \
            _code \
        }

    [...]

        int array[512]; 
        ITERATE(int, array, 512,
             printf("[%d] = %d\n", currentIndex, currentValue); /* Imagine this code spanning 50 lines or so */
        )

     

    Seen that before... can be quite useful when used right. I myself have written a few pseudo-functions just like that (mostly to do math)



  •  @Zecc said:

    @ammoQ said:

    looks like a workaround to me 

    Please enlighten us.

    Stupid me. Missed the runtime-if around the compiletime #define



  • @Infidel said:

     if((gcAtlSlots+gcIntlSlots)!= MAX_ATL_NODE) //Atl+Intl slots = MaxSlots. If user defines wrongly then the error validation is here
     {
      #undef gcIntlSlots
      #define gcIntlSlots (MAX_ATL_NODE -gcAtlSlots)
     }

    I fear that this is only the tip of the iceberg, and whoever coded this is using pre-processor "variables" in runtime. WTF indeed.



  •  Wouldn't the compiler complain that there is no statement inside the curly brackets?



  • @savar said:

     Wouldn't the compiler complain that there is no statement inside the curly brackets?

    [monitor@somewhere lab]$ cat test.c
    #include <stdio.h>

    int main(int argc, char **argv)
    {
            if (1==1) {
                    #undef nothing
                    #define nothing 666
            }
    }
    [monitor@somewhere lab]$ gcc test.c
    [monitor@somewhere lab]$

    Nope.



  • <font size="2" face="courier new,courier">

    me@localhost:~$ gcc test.c</font>

    <font size="2" face="courier new,courier">test.c:9:2: warning: no newline at end of file
    </font>

    <font size="2" face="courier new,courier">me@localhost:~$ echo \</font>

    <font size="2" face="courier new,courier">> >> test.c</font>

    <font size="2" face="courier new,courier">me@localhost:~$ gcc test.c</font>

    <font size="2" face="courier new,courier">me@localhost:~$ gcc -Wall test.c</font>

    <font size="2" face="courier new,courier">test.c: In function ‘main’:
    test.c:9: warning: control reaches end of non-void function
    </font>

    <font size="2" face="courier new,courier">me@localhost:~$ joe test.c</font>

    <font size="2" face="courier new,courier">me@localhost:~$ cat test.c
    </font>

    <font size="2" face="courier new,courier">#include <stdio.h>

    int main(int argc, char **argv)
    {
      if (1==1) {
        #undef nothing
        #define nothing 666
      }
      return nothing;
    }
    me@localhost:~$ gcc -Wall test.c</font>

    <font size="2" face="courier new,courier">me@localhost:~$</font><font size="2" face="courier new,courier"> gcc -Wall -pedantic test.c</font>

    <font size="2" face="courier new,courier">me@localhost:~$</font><font size="2" face="courier new,courier"> </font><font size="2" face="courier new,courier">gcc -Wall -pedantic -Wextra test.c</font>

    <font size="2" face="courier new,courier">test.c:3: warning: unused parameter ‘argc’
    test.c:3: warning: unused parameter ‘argv’</font>
    <font size="2" face="courier new,courier">me@localhost:~$</font><font size="2" face="courier new,courier"> </font><font size="2" face="courier new,courier">fortune
    You will gain money by an immoral action.</font>



Log in to reply