10
AleCx04
3y

When it comes to the idea of programming and magic, or the comparison between software developers/engineers, computer scientists etc as magicians or wizards, nothing brings the idea much more close to hearth than the C programming language.

A while ago I read the R.A Salvatore books concerning Drizzt, the dark elf. I loved the books, have not continued reading them but I remember them vividly. There was one book in which a human magician came about wielding extremely explosive magic, humans were capable of channeling large amounts of it through explosive and unwieldly ends.

This is the same feeling I get from C

Consider:
int items[] = {1, 2, 3};
printf("Third : %i\n", 3[items]);

and fuck me if shit like the above is not dangerous, it makes sense, arrays have the first items of it server as the pointer address to a first element, doing the above operation returns the third element of the array of 3. But holy shit if I don't think this is dangerous and interesting as fuck

there are many more examples I have that I am finding through me fucking around with: language development (compiler, interpreter), kernel programming as well as net sec. C is the most powerful and devastating thing we have in our hands indeed.

Comments
  • 6
    int items[] = {1, 2, 3};
    printf("Third : %i\n", 3[items]);
    "doing the above operation returns the third element of the array of 3"

    Actually it doesn't, it's undefined behaviour due to the out of bounds access.
  • 2
    @Fast-Nop YES! Sorry you are right, I was pretty tired last night and forgot to put the actual third item inside of the array. The example I am talking about is:

    @highlight
    int items[] = {1, 2, 3, 4};
    printf("%i\n", 3[items]); // this is the one that correctly gives back the number at index 3
  • 2
  • 1
    @Fast-Nop btw bud, you are our C wiz here, do you know of other black magic items that exist inside of the language?

    I have a passion for C, even though I have literally no projects in real life to apply it other than my own academic interests.
  • 2
    @AleCx04 Duff's Device https://en.wikipedia.org/wiki/... is pretty crazy shit.

    Then there's computed goto as GCC/Clang extension where you have an array of function-local labels and can goto-jump via the array index - I'd love to see that adopted in the C standard.

    If you consider goto as already bad, then setjmp/longjmp for exception handling in C will probably blow your fuses. That lets you jump upwards in the call stack.

    What's also nice is how to abuse extern array declarations for faking #error in conjunction with sizeof. Basically, you declare an array as extern whose size is either 1 or -1, depending on whether the sizeof stuff boils out or not. Since it's extern, it won't take actual space if it compiles, it's just a useless declaration.

    Finally, you can even abuse the C preprocessor as static website generator, that's really sick: https://accu.org/journals/overload/...
  • 1
    @Fast-Nop I am stuck trying to understand duff's device, didn't even knew what loop unrolling was, looking at videos now.

    This shit is crazy, I love it and need more
  • 1
    @AleCx04 In a loop, you have the actual loop body which does the work, and the loop overhead at the end of the loop to check the loop condition and make a conditional branch back to the top of the loop.

    If the body is short, then the overhead is considerable. So what you do is copy-paste the body N times and run the loop only 1/N times. That saves you (N-1)/N of the overhead.

    However, what if the actual loop length isn't divisible by N? Then you have to deal with the leftover loop runs. You can append a second loop for that - e.g. if you unroll by N=8, you can AND the original loop length with 0x07 and use that as loop length for this second loop. Or you can use the Duff's Device hack.
Add Comment