Sometimes you have to admire the ingenuity



  •  This found in some code

     

    #define sizeof(x) 128

    #include ...

    #undef sizeof

    #undef sizeof

     

    I'm glad they undef'd that twice, just to make sure it didn't get stuck



  • It seems to imply that once you undefine it, you are defining it, so they want to undefine that. Just trying to get to a pure definition of undefine.

    Existential code is existential.



  • @spadgas said:

     This found in some code

     

    #define sizeof(x) 128

    #include ...

    #undef sizeof

    #undef sizeof

     

    I'm glad they undef'd that twice, just to make sure it didn't get stuck

    Maybe sizeof was defined a second time in the "..." include, so to keep the Universe in balance there was a need for two undefes.



  • @spadgas said:

     This found in some code

     

    #define sizeof(x) 128

    #include ...

    #undef sizeof

    #undef sizeof

    Is this C? If so, one wonders what happens with stuff like:

    char buffer[512];
    [...]
    if (fread(buffer, sizeof buffer, fp)){....


  • @PJH said:

    Is this C? If so, one wonders what happens with stuff like:



    char buffer[512];

    [...]

    if (fread(buffer, sizeof buffer, fp)){....

    If it's C then that's obviously a syntax error.



  •  The reason for this code is probablyTRWTF. Something badly messed up in the file they are including. How anyone found that munging sizeof() 'fixed' it might well be a story to rival Mel's.



  •  But 'sizeof' is a keyword and not a function. It's like undefining the 'for' or 'while' keywords. You're also not supposed to put brackets around the parameter to 'sizeof', unless it's a type.



  • @Kittemon said:

    @PJH said:
    Is this C? If so, one wonders what happens with stuff like:



    char buffer[512];

    [...]

    if (fread(buffer, sizeof buffer, fp)){....

    If it's C then that's obviously a syntax error.

    Absent the the defines, my code is perfectly valid C.



  • @PJH said:

    @Kittemon said:
    @PJH said:
    Is this C? If so, one wonders what happens with stuff like:



    char buffer[512];

    [...]

    if (fread(buffer, sizeof buffer, fp)){....

    If it's C then that's obviously a syntax error.

    Absent the the defines, my code is perfectly valid C.
    Seems the undef is ignored (at least with GCC):

    [me@thinkpad-pjh tmp]$ cat sizeof.c
    #include <stdio.h>
    #undef sizeof
    int main(void){
            char buffer[512];
            printf("%d\n", sizeof buffer);
            return 0;
    }
    

    [me@thinkpad-pjh tmp]$ make -B sizeof
    cc sizeof.c -o sizeof
    [me@thinkpad-pjh tmp]$ ./sizeof
    512
    [me@thinkpad-pjh tmp]$



  • Apart from previous example not actually using the sizeof(x) macro,

     

    I can think of the [nightmare] pSOS operating system where if you asked for an object whose size was just under 64 bytes, and you had not preallocated a pool of 32 to 64 byte objects, the call to malloc(45) would return 0. 

    If you had a preallocated pool of 128 to 256 byte objects with a free item then on the same system a call to malloc(128)  would return a valid pointer. 

    Perhaps the user was trying to get around something like that with the definition.

     



  • @PJH said:

    Absent the the defines, my code is perfectly valid C.
     

    Except for the fact that fread() takes four parameters.



  • @PJH said:

    @spadgas said:

     This found in some code

     

    #define sizeof(x) 128

    #include ...

    #undef sizeof

    #undef sizeof

    Is this C? If so, one wonders what happens with stuff like:

    char buffer[512];
    [...]
    if (fread(buffer, sizeof buffer, fp)){....

    I'm so glad this kind of code is not my fread and buffer...


    (Then again, I work in VB and PHP. Maybe #define true FILE_NOT_FOUND would be an improvement.)



  • @Medinoc said:

    Except for the fact that fread() takes four parameters.
    Oh bother...



  •  +1 : genuine, eyes-wide, shout-out-loud WTF.

    I would say this would be good front-page material, but it's simply better than that.



  • I have done stuff like that to override malloc and so on, in order to analyze how much dynamic memory is being used by a program.



  • @zzo38 said:

    I have done stuff like that to override malloc and so on, in order to analyze how much dynamic memory is being used by a program.
    Valgrind.



  • @PJH said:

    Valgrind.

    Yeah, if you're working on a platform it supports.



  • @PJH said:

    @Kittemon said:
    @PJH said:
    Is this C? If so, one wonders what happens with stuff like:



    char buffer[512];

    [...]

    if (fread(buffer, sizeof buffer, fp)){....

    If it's C then that's obviously a syntax error.

    Absent the the defines, my code is perfectly valid C.

    @lettucemode said:

    First of all, tags.

    Secondly, would it matter? IIRC foo(x) and foo (x) are different for a suitable parameterized definition of foo.



  • @PJH said:

    Seems the undef is ignored (at least with GCC)

    #undef foo is always ignored for non-defined foo.



  • @Kittemon said:

    @PJH said:
    Seems the undef is ignored (at least with GCC)

    #undef foo is always ignored for non-defined foo.

    One of those cases where "do nothing" is exactly the right implementation.  Compiler sez: "You want I should arrange it so foo is not defined?  Gotcha covered, boss!"


Log in to reply
 

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