Any good idioms for error handling in straight C programs?

后端 未结 12 1513
梦如初夏
梦如初夏 2020-12-23 14:19

Getting back in to some C work.

Many of my functions look like this:

int err = do_something(arg1, arg2, arg3, &result);

With th

12条回答
  •  -上瘾入骨i
    2020-12-23 14:58

    You can get really silly and do continuations:

    void step_1(int a, int b, int c, void (*step_2)(int), void (*err)(void *) ) {
         if (!c) {
             err("c was 0");
         } else {
             int r = a + b/c;
             step_2(r);
         }
    }
    

    This probably isn't actually what you want to do, but it is how many functional programming languages are used, and even more often how they model their code for optimization.

提交回复
热议问题