By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,556 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,556 IT Pros & Developers. It's quick & easy.

What's the purpose of using variable list arguments in error handling?

P: n/a
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

Jun 15 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
I meant to say "Variable-length Argument Lists".

Jun 15 '07 #2

P: n/a
On Jun 14, 8:59 pm, Chad <cdal...@gmail.comwrote:
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?

My error handling usually involves a lot more than a single printf().
Not least writing to the log server, or send some notification back to
the user. And of course the common log routines often add context
information (who, when, where) to the logged item. In that case, you
may be executing dozens or hundreds of lines of code. And being able
to pass a variable argument list into that is certainly helpful.

Plus, even if all you're doing is a printf() and exit(), is a good
idea to centralize the code in case you want to improve that in the
future...

Jun 15 '07 #3

P: n/a
On Jun 15, 1:59 pm, Chad <cdal...@gmail.comwrote:
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*/

}
This code does a lot more than the original, so you are
comparing apples and oranges.

An apples-to-apples comparison might be:
void err_exit( char const *fmt, ... )
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
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.
If you delete all the newlines then you could get
it all on one line!

(Hint: why are you using line count as a measure of code quality).
So is there some kind of magic when using variable argument lists for
error handling?
What was wrong with your original code exactly?

Jun 15 '07 #4

P: n/a
On Jun 14, 8:34 pm, Old Wolf <oldw...@inspire.net.nzwrote:
On Jun 15, 1:59 pm, Chad <cdal...@gmail.comwrote:
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*/
}

This code does a lot more than the original, so you are
comparing apples and oranges.

An apples-to-apples comparison might be:
void err_exit( char const *fmt, ... )
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
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.

If you delete all the newlines then you could get
it all on one line!

(Hint: why are you using line count as a measure of code quality).
So is there some kind of magic when using variable argument lists for
error handling?

What was wrong with your original code exactly?

I don't think anything is really wrong witht he code. I'm trying to
clean some large code that I wrote a while back. I on the three step
minimum wage clerk approach.

1)Just get the program to solve the given problem.
2)Add more error handling as you start to find various ways to crash
the program before releasing the program. Example. Inputting junk
data, inputting data that exceeds the buffer length, etch.
3)Clean up the code. This is the stage I'm at now.

Actually, when I think about this. I think it was the former CTO of
netgensis that told me he used this three step approach when he coded
stuff.
Jun 15 '07 #5

P: n/a
In article <11**********************@j4g2000prf.googlegroups. com>,
Chad <cd*****@gmail.comwrote:
>void err_exit(const char *fmt, ...){
The point of this is so that you can do, for example,

if(year < 1960 || year 2100)
err_exit("date %d out of range", year);

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 15 '07 #6

P: n/a
On Jun 15, 3:30 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <1181872772.536733.303...@j4g2000prf.googlegroups. com>,

Chad <cdal...@gmail.comwrote:
void err_exit(const char *fmt, ...){

The point of this is so that you can do, for example,

if(year < 1960 || year 2100)
err_exit("date %d out of range", year);

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.

Okay. I wasn't aware of that.

Jun 15 '07 #7

P: n/a
>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.
Using a "line of code" as a unit of measurement is a mistake. It's
worse than measuring air or gasoline by the "heaping bagful" (any
size bag). Also, management might see it and think it means
something.

The above code could appear on one line. Or it could be written:

fprintf
(
stderr
,
"malloc failed"
)
;
exit
(
EXIT_FAILURE
)
;

and now it's 12 lines.

>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*/
}
All of the code above could fit on 1 line except the preprocessor
directive.
>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);
Missing double quote above.

Or it could be replaced with 5 lines of code:
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.
Counting lines of code is a waste of time.
>So is there some kind of magic when using variable argument lists for
error handling?
My advice is to stop counting lines of code.

Jun 16 '07 #8

P: n/a
Gordon Burditt wrote:
>
>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.

Using a "line of code" as a unit of measurement is a mistake.
It's worse than measuring air or gasoline by the "heaping bagful"
(any size bag). Also, management might see it and think it means
something.

The above code could appear on one line. Or it could be written:

fprintf
(
stderr
,
"malloc failed"
)
;
exit
(
EXIT_FAILURE
)
;

and now it's 12 lines.
.... snip ...
>
All of the code above could fit on 1 line except the preprocessor
directive.
And you think this doesn't happen in commercial code? Especially
where the management counts lines.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 17 '07 #9

P: n/a
CBFalconer wrote, On 17/06/07 01:03:
Gordon Burditt wrote:
>>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.
Using a "line of code" as a unit of measurement is a mistake.
It's worse than measuring air or gasoline by the "heaping bagful"
(any size bag). Also, management might see it and think it means
something.

The above code could appear on one line. Or it could be written:

fprintf
(
stderr
,
"malloc failed"
)
;
exit
(
EXIT_FAILURE
)
;

and now it's 12 lines.
... snip ...
>All of the code above could fit on 1 line except the preprocessor
directive.

And you think this doesn't happen in commercial code? Especially
where the management counts lines.
Sometimes it might, sometimes it does not. Where I used to work it did
not happen, at least not enough to have a noticeable effect. This was
probably because it was only used as a very rough measure of comparative
size/complexity by people with enough knowledge to know the problems in
such a measure. Also the code underwent reviews, so things like shown
above would be changed to a more sane layout :-)
--
Flash Gordon
Jun 17 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.