Tuesday, June 21, 2016

How to make assertion failures more useful in C

It's common for a C program to use the assert(3) routine to guarantee that it will abort when an invalid situation happens. For example:
ret = foo();
assert(ret == 0);
If that condition is false, the program will exit, print an error saying that this assertion was hit and generate a core dump (if the system is configured to). It can make life a bit easier if we add an error string to the same check:
ret = foo();
assert(ret == 0 || "foo() returned non-zero when called from bar()");

No comments:

Post a Comment