When you want the right address...



  • Say you have an array of something:

    int myarray[200];

    or maybe you have

    int* array2 = new int[200];

     
    I thought I'd only ever see what I'll show next at one particular workplace, but I just came across it in code I was asked to rewrite for a university research project.  To pass these arrays to a function, void myfunc(int* thelist), you would think the course of action would be:

    myfunc(myarray);

    myfunc(array2);

    This is what made (makes?) me go WTF:

    myfunc(&myarray[0]);

    myfunc(&array2[0]);

    The first time I saw that I stared at it for ten minutes before I realized what the author(s) should have done...
     



  • I do that occasionally, depending on what I'm doing in the function.

    If I want to operate on the first value in the array, and only on that first value, I will pass &array[0] the same way I'd pass &array[5] or even &a.

    However, if I am doing something with the array itself, I'll pass the array itself as a parameter, myfunc(array).

    In your post, however, you're doing the latter, so yeah, it looks like some brute-force ignorance was at work. I think that name deserves a tag!
     



  • That's not particularly unusual practice if you use STL vectors (or any other custom array type that provides an element reference through indexing) frequently and want to keep notation consistant between static arrays and STL vectors, since it's the only common syntactical denominator.  So it's not an uncommon habit and there's nothing wrong with it really.  Getting the address of an element as an element pointer is about as grammatically correct as you can get (as opposed to implicitly converting array types to element pointers, which most higher level languages don't even allow).



  • Actually,

    you can do odd things with arrays in C / C++. Supose you have int * array and have allocated memory for it (the same as int array[<slots>])

    Then, you can index it the normal way:

    array[index]

    ... or the odd way:

    index[array] //(try it, it works!)

    This, because array[index] is translated by the compilator to *(array + index), which is the same, obviously, as *(index + array) = index[array]

     



  • @pamaramb said:

    Actually,

    you can do odd things with arrays in C / C++. Supose you have int * array and have allocated memory for it (the same as int array[<slots>])

    Then, you can index it the normal way:

    array[index]

    ... or the odd way:

    index[array] //(try it, it works!)

    This, because array[index] is translated by the compilator to *(array + index), which is the same, obviously, as *(index + array) = index[array]

     

    Fun way to confuse new c++ programmers by using 2["Constant String"] to get the character n in this example 🙂 



  • This is actually a fairly common practice in some quarters, especially among older coders. An elder master at my current coding dojo said that he worked with old broken compilers that wouldn't recognize that &array[0] == array.

     



  • The guy who wrote this barely knows C, let alone C++/STL 😉



  • @Heron said:

    The guy who wrote this barely knows C, let alone C++/STL 😉

    It's still neither an uncommon practice nor unintuitive (taking the address of the first element seems intuitive to someone who thinks "I need to pass the address of the first element").  It's not WTF at all.



  • int* array2 = new int[200];

    Wait.

    An array of refs to ints of length 200?

    How does an int have length 200?

    (I = not a C++ programmer)



  • @dhromed said:

    int* array2 = new int[200];

    Wait.

    An array of refs to ints of length 200?

    How does an int have length 200?

    (I = not a C++ programmer)

    int* array2 = (int*)calloc(sizeof(int), 200);

    Equivalent for C 😉 



  • Yeah, that clears things up.

    Looked up calloc() via google.

    From what I can tell it picks up the memrory slack left by ints of length 200.

    Reuh.

     



  • Fun abuse of that in base conversion...

    /* Tried to make it as obfuscated as possible... */
    char *toRadix(int num, unsigned char radix) {
    int bufsz = (((int)(log(num)/log(radix)))+1+!!(num<0)), i;
    char *buf = (char*)malloc(bufsz);
    if (num < 0) {
    buf[0] = '-';
    num = -num;
    }
    buf[bufsz-1] = '\0';
    for (i=bufsz-2;i>=0;--i) {
    buf[i] = (num%radix)["0123456789abcdefghijklmnopqrstuvwxyz"];
    num /= radix;
    }
    return buf;
    }

    Completely untested, YMMV.
     



  • @Fred Foobar said:

    Fun abuse of that in base conversion...

    [code]

    /* Tried to make it as obfuscated as possible... */ 

    char *toRadix(int num, unsigned char radix) {

       int bufsz = (((int)(log(num)/log(radix)))+1+!!(num>0)), i;

       char buf = (char)malloc(bufsz);

       if (num < 0) {

           buf[0] = '-';

           num = -num;

       }

       buf[bufsz-1] = '\0';

       for (i=bufsz-2;i>=0;--i) {

          buf[i] = (num%radix)["0123456789abcdefghijklmnopqrstuvwxyz"];

          num /= radix;

       }

       return buf;

    }

    [/code]

    Completely untested, YMMV.


    That could be clearer...



  • @Thief^ said:

    @Fred Foobar said:

    Fun abuse of that in base conversion...

    [code]

    /* Tried to make it as obfuscated as possible... */ 

    char *toRadix(int num, unsigned char radix) {

       int bufsz = (((int)(log(num)/log(radix)))+1+!!(num>0)), i;

       char buf = (char)malloc(bufsz);

       if (num < 0) {

           buf[0] = '-';

           num = -num;

       }

       buf[bufsz-1] = '\0';

       for (i=bufsz-2;i>=0;--i) {

          buf[i] = (num%radix)["0123456789abcdefghijklmnopqrstuvwxyz"];

          num /= radix;

       }

       return buf;

    }

    [/code]

    Completely untested, YMMV.


    That could be clearer...

    Hey, I said flat out that I tried to make it as obfuscated as possible...

    But really, the first time around, the forum software choked and died on the <code> tag. I had to go into manual HTML mode to clean it up. It's better now. 



  • The best part is that it shows up perfectly fine in the message editor (quote it and see), but is completely shot in the preview.



  • Obfuscation complete!


Log in to reply
 

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