Jun 19, 2011

I use int 3 for assert()

C has assert() function.  Sure.. I don't use it.  I don't like the call stack it gives me.  It is rather confusing: I want the line with the problem at the top of my call stack. Sure, there's a way to unroll stack for sure. Too much hassle. Instead, I use this for my own assert.

#define ASSERT(expr, ...) if(!expr) __asm{ int 3 }

What it does is basically breaking at the code where this ASSERT happens.  So when I use this assert I would do something like this:

ASSERT( life == sucks, "LIFE CAN ONLY SUCK");

This string message is for my own reference.  When the code breaks in debugger, it simply shows that code line, so I know what it is right away.

As it relies on HW interrupt, it only works on PC.  If you want this to work on PowerPC CPUs, I heard you have to use this instead.

#define ASSERT(expr, ...) if(!expr) asm{trap}

Happy coding. Yay?

Jun 9, 2011

Inside of char* string buffer initialization

I used to initialize a string buffer this way:

char temp[64];
temp[0] = 0;

A few years ago, one of my coworkers at my previous studio, Capcom Vancouver, told me the following way is better:

char temp[64] = {0, };

I don't remember why he said it was better. I've been just using this way since I'm a nice guy who trusts one's coworkers.  But finally I figured it out.... well... by accident...

The other day I was doing some profile captures on Xbox 360 and I happened to see how the above code gets compiled into. Once compiled, it turns into this:

char temp[64];
memset(temp, 0, sizeof(char) * 64);

Interesting, huh? It takes a few micro secs, so not that bad, but now I think this type of initialization is not always necessary if the buffer is always filled by strcpy or similar functions right after. (so as long as it eventually becomes null-terminated)