473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
9 2561
I meant to say "Variable-length Argument Lists".

Jun 15 '07 #2
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
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
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
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
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
>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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
46
by: Reinhold Birkenfeld | last post by:
Hello, another Perl/Python question: the subject says it all. Perl is going to change dramatically to become a more powerful and easier to (read|write) language. Is Python taking a similar...
7
by: Kapt. Boogschutter | last post by:
I'm trying to create a function that has at least 1 Argument but can also contain any number of Arguments (except 0 because my function would have no meaning for 0 argument). The arguments...
7
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double...
18
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
1
by: ohaqqi | last post by:
Hi guys, I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows: > type <file1> This command will implement a catenation of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.