Skip to content

Conversation

screaminglineage
Copy link

The main issue this solves is that when the assert() from libc is called in a debugger, you end up several stack levels deep and have to hunt down where the crash actually occurred.

It ends up looking like this.
image

This approach instead makes the compiler generate a trap instruction and crash immediately.

PS: You could also implement cool custom formatting for asserts, but I didn't put that in for the sake of keeping it simple.

#define assertf(expr, ...)    \
    (!(expr))?     \
        (printf("%s:%d: assertion `%s` failed: \"", __FILE__, __LINE__, #expr),    \
        printf(__VA_ARGS__),  \
        printf('\"\n'),  \ 
        CRASH())    \ 
    : (void)0

assertf(a == b, "expected %d but got %d", a, b);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant