C
Chad
This might be a bit vague and poorly worded.....
In my program, I handle function failures using fprintf() and exit()
like:
fprintf(stderr, "malloc failed");
exit(EXIT_FAILURE);
There are 5 of these. Since each one has two lines, the total lines of
code would be 10. Now if I would use variable argument lists for my
error functions, I would use something like
#include <stdarg.h>
void err_exit(const char *fmt, ...){
va_list ap;
va_start(ap, fmt);
err_doit(1,errno,fmt, ap);
exit(1);
}
static void err_doit(int errnoflag, int error, const char *fmt,
va_list *ap){
/*more code here*/
}
Then all 2 line I used for error handling
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
would be replaced with 1 line of error handling.
err_exit("malloc failed);
This means I would have only 5 lines of code to do the error handling.
But an additional 7 plus lines of code for the error handling function
itself. Or a total of 12 lines of code. This is NOT a net savings
since my original code only had a total of 10 lines.
So is there some kind of magic when using variable argument lists for
error handling?
Chad
In my program, I handle function failures using fprintf() and exit()
like:
fprintf(stderr, "malloc failed");
exit(EXIT_FAILURE);
There are 5 of these. Since each one has two lines, the total lines of
code would be 10. Now if I would use variable argument lists for my
error functions, I would use something like
#include <stdarg.h>
void err_exit(const char *fmt, ...){
va_list ap;
va_start(ap, fmt);
err_doit(1,errno,fmt, ap);
exit(1);
}
static void err_doit(int errnoflag, int error, const char *fmt,
va_list *ap){
/*more code here*/
}
Then all 2 line I used for error handling
fprintf(stderr, "malloc failed\n");
exit(EXIT_FAILURE);
would be replaced with 1 line of error handling.
err_exit("malloc failed);
This means I would have only 5 lines of code to do the error handling.
But an additional 7 plus lines of code for the error handling function
itself. Or a total of 12 lines of code. This is NOT a net savings
since my original code only had a total of 10 lines.
So is there some kind of magic when using variable argument lists for
error handling?
Chad