error handling strategies in C?
- by Leo
Given the code below:
typedef struct {int a;} test_t;
arbitrary_t test_dosomething(test_t* test) {
if (test == NULL) {
//options:
//1. print an error and let it crash
//e.g. fprintf(stderr, "null ref at %s:%u", __FILE__, __LINE__);
//2. stop the world
//e.g. exit(1);
//3. return (i.e. function does nothing)
//4. attempt to re-init test
}
printf("%d", test->a); //do something w/ test
}
I want to get a compiler error if test is ever NULL, but I guess that's not possible in C. Since I need to do null checking at runtime, what option is the most proper way to handle it?