Tuesday, 22 February 2011

Whyyyyyyyy???

I spent an hour of debugging and trying to understand what the hell is happening and why on this strange thing:

// hit->distance = calcFirstIntersection(player, angle, intersection_type);\
hit->distance = 512;

printf("%d\n", hit->distance);


It was supposed to print 512 but it was printing 32765 and 34. I realized that something was going completely wrong because I have mistakenly typed the character '\' after the commented line (probably because those stupid keyboards have this character over half the 'Enter' key).

After trying to understand why that character even when it's commented have such result and what is actually happening, I found out that it cancelled my next line. Not exactly. It actually cancels CR+LF or something like that.

Simply, if I type:

printf("foo\
bar");


It is like I am typing:

printf("foobar");

So, the accidentally typed '\', even after a comment, brought my next line in the same commented line, thus I wasn't loading hit->distance with anything and my printf got what was on the init of this value or something random anyway.


I know I have seen this '\' symbol used somewhere before. When people write inline assembly in some C compilers. Oh, also in some code that passed a GLSL shader as a string. The whole thing was probably needed to be given in the same line but \ would make it possible to have it line after line I guess.

I know this is old story for some of you but the only reason I am writting this post is because I was sooooo frustrated and actually perplexed by this very very strange thing (it is strange when you didn't know it's use before and it even worked after the comment) that I wanted to write anything.

FUUUUUUUUUUUU

p.s. Ok,. back to code. Lot's of work was done in the wolfenstein CPC engine the last few days. I have something that works on PC, almost doesn't work on CPC and it's slow like hell. If I manage to make it work there, I will need another pair of effort to optimize it. It's gonna be a hell of a hard work.

2 comments:

  1. Yes, it's actually a very common idiom to split big macros into multiple lines, by using \ characters at the end of line which merge the lines back together.

    For instance I always do the following:

    #define PACK_RGBA(r, g, b, a) \
        (((a & 0xff) << 24) | \
         ((r & 0xff) << 16) | \
         ((g & 0xff) << 8) | \
         (b & 0xff))

    Or something like:

    #define NORMALIZE(v) \
        do { \
            float mag = sqrt((v)[0] * (v)[0] + (v)[1] * (v)[1] + (v)[2] * (v)[2]); \
            (v)[0] /= mag; \
            (v)[1] /= mag; \
            (v)[2] /= mag; \
        } while(0)

    ReplyDelete
  2. Ah I see. It's useful for such macros. I haven't written a big macro before but now I know.

    p.s. This week I had a quite productive coding week but I encounter the most facepalm bugs in such a short period. Never before has it happened with such frequency. You know, bugs that bother you for an hour or so and then you discover it was something very stupid or unexpected :)

    ReplyDelete