Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices to use assert and printf together

Tags:

c

I'm currently using the following statement to get assert and printf to work together, is there any better way to do this?

#define ASSERT(x) for(; !(x); assert(x))
#define ALLOC_CHECK(x, ...) ASSERT(x) printf(__VA_ARGS__ "\n");
ALLOC_CHECK(ptr != NULL, "Pointer not initialized");
like image 805
user3666471 Avatar asked May 13 '26 17:05

user3666471


1 Answers

You don't need to whip up your own assert() variant, you can just include an error message in the assert itself:

assert(someCondition && "Internal error: ... . Please report this bug.");

Since the assert() macro prints its argument on failure, it will also print your string.

like image 167
cmaster - reinstate monica Avatar answered May 15 '26 07:05

cmaster - reinstate monica