473,699 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_FAILU RE);

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,errn o,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_FAILU RE);

would be replaced with 1 line of error handling.
err_exit("mallo c 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 2582
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_FAILU RE);

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,errn o,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_FAILU RE);

would be replaced with 1 line of error handling.
err_exit("mallo c 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_FAILU RE);

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,errn o,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_FAILU RE);
}
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...@inspir e.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_FAILU RE);
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,errn o,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_FAILU RE);
}
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************ **********@j4g2 000prf.googlegr oups.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
--
"Considerat ion 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.536 733.303...@j4g2 000prf.googlegr oups.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
--
"Considerat ion 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_FAIL URE);

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,err no,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_FAIL URE);

would be replaced with 1 line of error handling.
err_exit("mall oc 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_FAIL URE);

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.securityfoc us.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(stder r, "malloc failed");
exit(EXIT_FAI LURE);

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
19039
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 any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
46
3281
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 step (-> Python 3) some time in the near future? Reinhold
7
6289
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 passed to the function are strings or must be (automaticly converted to a string e.g. the number 10 should become the string "10". My problem is that I can only find samples and description of printf() like functions where the optional arguments and...
7
8319
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 AFunct2 ( int num, ... ); double average ( int num, ... )
18
2094
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
13
5040
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
3627
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 so-called pre-fix notation or algebraic notation. Algebraic notations have the concept of operators, meaning, symbols placed around arguments. In algebraic in-fix notation, different
4
6690
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 = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
1
8255
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 file1, equal to the command cat <file1> I need to use execvp() and fork() system calls to create a new process that will type/cat any text file. In my code below, the execvp() call in the function at the bottom gets me the following: ...
0
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9174
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8883
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.