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?

1 comment:

  1. You can read http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/ for several improvements to the assert macro, including a variant of yours.