Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

Add NOB_ASSERT that doesnt depend on assert.h#105

Open
screaminglineage wants to merge 3 commits intotsoding:main from
screaminglineage:nob-assert
Open

Add NOB_ASSERT that doesnt depend on assert.h #105
screaminglineage wants to merge 3 commits intotsoding:main from
screaminglineage:nob-assert

Conversation

@screaminglineage
Copy link

@screaminglineage screaminglineage commented Aug 21, 2025

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);

rexim and jmnuf reacted with thumbs up emoji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /