Last active
September 5, 2022 23:18
-
-
Save bergkvist/c0157d140ca00f75477dcf97d5638133 to your computer and use it in GitHub Desktop.
Useful assertion macros in C which include debug information like file, line number and error message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// These require compiler support for statement expressions (https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html) | |
#define assert_ok(e) ({int x = (e); if (x < 0) { printf("%s:%d: ", __FILE__, __LINE__); fflush(stdout); perror(#e); abort(); } x;}) | |
#define assert_ptr(e) ({void* p = (e); if (p == NULL) { printf("%s:%d: %s: NULL pointer returned\n", __FILE__, __LINE__, #e); abort(); } p;}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment